Tcl Source Code

Artifact [3f76849698]
Login

Artifact 3f76849698560bdfa5ea97fd61e86c2060a520f7:

Attachment "teovi-13Jun06.patch" to ticket [1290457fff] added by tallniel 2006-06-14 02:37:08.
Index: generic/tclBasic.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclBasic.c,v
retrieving revision 1.194
diff -u -r1.194 tclBasic.c
--- generic/tclBasic.c	4 May 2006 12:55:49 -0000	1.194
+++ generic/tclBasic.c	13 Jun 2006 18:45:49 -0000
@@ -35,6 +35,24 @@
 } OldMathFuncData;
 
 /*
+ * The following structure is used to pass information between then
+ * TclEvalObjvInternal group of functions. It should be considered private to
+ * those functions.
+ */
+typedef struct TclEvalObjvInternalData {
+    CONST char      *command;   /* Command invocation string */
+    int             length;     /* Length of command string */
+    Command         *cmdPtr;    /* Corresponding Command structure */
+    int             objc;       /* Number of objects in command invocation */
+    Tcl_Obj * CONST *objv;      /* Actual objects of command invocation */
+    int             code;       /* Return code from invoking command */
+    int             traceCode;  /* Return code from execution traces */
+    int             flags;      /* TCL_EVAL_GLOBAL etc */
+    CallFrame       *savedVarFramePtr; /* Saved call frame and namespace context.*/
+    Namespace       *savedNsPtr;
+} TclEvalObjvInternalData;
+
+/*
  * Static functions in this file:
  */
 
@@ -3195,6 +3213,309 @@
 /*
  *----------------------------------------------------------------------
  *
+ * TclEvalObjvInternalUnknownHandler --
+ *
+ * 	Called to handle an unknown command being invoked.  This calls a
+ * 	procedure called "::unknown" if it exists, or generates a standard
+ * 	error message.
+ *
+ * Results:
+ * 	The return value is the result of calling the ::unknown procedure,
+ * 	or TCL_ERROR if no such procedure exists.
+ *
+ * Side effects:
+ * 	Depends on the ::unknown command. Alters fields in "data".
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+TclEvalObjvInternalUnknownHandler(
+    Tcl_Interp 			*interp,
+    TclEvalObjvInternalData	*data)
+{
+    Namespace *currNsPtr = NULL;    /* Used to check for and invoke any
+                                     * registered unknown command handler
+                                     * for the current namespace (TIP 181). */
+    int newObjc, handlerObjc;
+    Tcl_Obj **handlerObjv, **newObjv;
+    Command *cmdPtr;
+    Interp *iPtr = (Interp *)interp;
+    int i;
+
+    if (iPtr->varFramePtr != NULL) {
+        currNsPtr = iPtr->varFramePtr->nsPtr;
+    }
+    if ((currNsPtr == NULL) || (currNsPtr->unknownHandlerPtr == NULL)) {
+        currNsPtr = iPtr->globalNsPtr;
+    }
+    if (currNsPtr == NULL) {
+        Tcl_Panic(
+        "TclEvalObjvInternalUnknownHandler: NULL global namespace pointer");
+    }
+    if (currNsPtr->unknownHandlerPtr == NULL) {
+        /* Global namespace has lost unknown handler; reset. */
+        currNsPtr->unknownHandlerPtr = Tcl_NewStringObj("::unknown", -1);
+        Tcl_IncrRefCount(currNsPtr->unknownHandlerPtr);
+    }
+    Tcl_ListObjGetElements(NULL, currNsPtr->unknownHandlerPtr,
+        &handlerObjc, &handlerObjv);
+    newObjc = data->objc + handlerObjc;
+    newObjv = (Tcl_Obj **) ckalloc((unsigned) (newObjc * sizeof(Tcl_Obj *)));
+
+    /* Copy command prefix from unknown handler. */
+    for (i = 0; i < handlerObjc; ++i) {
+        newObjv[i] = handlerObjv[i];
+        Tcl_IncrRefCount(newObjv[i]);
+    }
+    /* Add in command name and arguments. */
+    for (i = data->objc-1; i >= 0; --i) {
+        newObjv[i+handlerObjc] = data->objv[i];
+    }
+
+    cmdPtr = (Command *) Tcl_GetCommandFromObj(interp, newObjv[0]);
+
+    if (cmdPtr == NULL) {
+	Tcl_AppendResult(interp, "invalid command name \"",
+		Tcl_GetString(data->objv[0]), "\"", NULL);
+	data->code = TCL_ERROR;
+    } else {
+	iPtr->numLevels++;
+	data->code = TclEvalObjvInternal(interp, newObjc, newObjv,
+		data->command, data->length, 0);
+	iPtr->numLevels--;
+    }
+    for (i = 0; i < handlerObjc; ++i) {
+        Tcl_DecrRefCount(newObjv[i]);
+    }
+    ckfree((char *) newObjv);
+
+    if (data->savedNsPtr) {
+        iPtr->varFramePtr->nsPtr = data->savedNsPtr;
+    }
+
+    return data->code;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclEvalObjvInternalTraceEnter --
+ *
+ * 	Fires execution traces before executing a command.
+ *
+ * Results:
+ * 	Returns the result of any execution enter traces, or TCL_OK if there
+ * 	are none.
+ *
+ * Side effects:
+ * 	Execution traces may cause the command being invoked to be changed
+ * 	in some way, so it may have to be looked up again if
+ * 	data->cmdPtr->cmdEpoch has changed.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+TclEvalObjvInternalTraceEnter(
+    Tcl_Interp 			*interp, 
+    TclEvalObjvInternalData	*data)
+{  
+    Interp *iPtr = (Interp *)interp;
+
+    data->cmdPtr->refCount++;
+
+    if (iPtr->tracePtr != NULL && data->traceCode == TCL_OK) {
+	data->traceCode = TclCheckInterpTraces(interp, 
+		data->command, data->length,
+		data->cmdPtr, data->code, TCL_TRACE_ENTER_EXEC, 
+		data->objc, data->objv);
+    }
+    if ((data->cmdPtr->flags & CMD_HAS_EXEC_TRACES) && 
+        (data->traceCode == TCL_OK)) {
+	data->traceCode = TclCheckExecutionTraces(interp, 
+		data->command, data->length,
+		data->cmdPtr, data->code, TCL_TRACE_ENTER_EXEC, 
+		data->objc, data->objv);
+    }
+    data->cmdPtr->refCount--;
+
+    return data->traceCode;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclEvalObjvInternalLookupCommand --
+ *
+ * 	Locates the Command structure corresponding to the invocation in
+ * 	data->objv.  Takes care of firing execution enter traces and
+ * 	adjusting the lookup as a result.  If the command cannot be found,
+ * 	then invokes the UnknownHandler.
+ *
+ * Results:
+ * 	The Command structure is left in data->cmdPtr.
+ *
+ * Side effects:
+ * 	Fires any execution enter traces associated with the command, as
+ * 	these may affect the lookup process.
+ *
+ *----------------------------------------------------------------------
+ */
+
+void
+TclEvalObjvInternalLookupCommand(
+    Tcl_Interp 			*interp,
+    TclEvalObjvInternalData	*data)
+{
+    Interp *iPtr = (Interp *)interp;
+    int checkTraces;
+    int cmdEpoch;
+
+    /*
+     * Configure evaluation context to match requested flags.
+     */
+    data->savedVarFramePtr = iPtr->varFramePtr;
+    if (data->flags & TCL_EVAL_GLOBAL) {
+	iPtr->varFramePtr = NULL;
+    } else if ((data->flags & TCL_EVAL_INVOKE) && iPtr->varFramePtr) {
+	data->savedNsPtr = iPtr->varFramePtr->nsPtr;
+	iPtr->varFramePtr->nsPtr = iPtr->globalNsPtr;
+    }
+
+    /*
+     * Lookup the command. If it is unknown then invoke an unknown command
+     * handler.
+     *
+     * If any execution traces rename or delete the current command, we may
+     * need (at most) two passes here.
+     */
+    for (checkTraces = 1; checkTraces >= 0; --checkTraces) {
+
+	data->cmdPtr = (Command *) 
+            Tcl_GetCommandFromObj(interp, data->objv[0]);
+
+	if (data->cmdPtr == NULL) {
+	    /*
+	     * Unknown command.
+	     */
+	    TclEvalObjvInternalUnknownHandler(interp, data);
+	    break;
+	} else if (checkTraces) {
+            if (data->savedNsPtr) {
+                iPtr->varFramePtr->nsPtr = data->savedNsPtr;
+            }
+
+            /*
+             * Call trace functions if needed.
+             */
+
+	    cmdEpoch = data->cmdPtr->cmdEpoch;
+	    
+	    TclEvalObjvInternalTraceEnter(interp, data);
+
+	    if (cmdEpoch == data->cmdPtr->cmdEpoch) {
+		/* Trace didn't alter command, so avoid re-lookup */
+		break;
+	    }
+	}
+    } 
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclEvalObjvInternalInvokeCommand --
+ *
+ *	Takes care of actually invoking a command, found previously by
+ *	TclEvalObjvInternalLookupCommand.  Assumes data->cmdPtr is non-NULL.
+ *
+ * Results:
+ * 	Sets data->code to result of command invocation, and also returns
+ * 	this result.
+ *
+ * Side effects:
+ * 	Depends on command.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+TclEvalObjvInternalInvokeCommand(
+    Tcl_Interp 			*interp,
+    TclEvalObjvInternalData	*data)
+{
+    Interp *iPtr 	= (Interp *)interp;
+
+    data->cmdPtr->refCount++;
+    iPtr->cmdCount++;
+
+    if (data->code == TCL_OK && data->traceCode == TCL_OK && 
+	!Tcl_LimitExceeded(interp)) {
+
+	if (!(data->flags & TCL_EVAL_INVOKE) &&
+		(iPtr->ensembleRewrite.sourceObjs != NULL) &&
+		!Tcl_IsEnsemble((Tcl_Command) data->cmdPtr)) {
+	    iPtr->ensembleRewrite.sourceObjs = NULL;
+	}
+	data->code = (*data->cmdPtr->objProc)
+	    (data->cmdPtr->objClientData, interp, data->objc, data->objv);
+    }
+    if (Tcl_AsyncReady()) {
+	data->code = Tcl_AsyncInvoke(interp, data->code);
+    }
+    if (data->code == TCL_OK && Tcl_LimitReady(interp)) {
+	data->code = Tcl_LimitCheck(interp);
+    }
+
+    return data->code;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclEvalObjvInternalTraceLeave --
+ *
+ * 	Fires execution leave traces for a command.
+ *
+ * Results:
+ * 	Returns result of executing traces, or TCL_OK if there were none.
+ *
+ * Side effects:
+ * 	Depends of execution traces.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+TclEvalObjvInternalTraceLeave(
+    Tcl_Interp			*interp,
+    TclEvalObjvInternalData	*data)
+{
+    Interp *iPtr 	= (Interp *)interp;
+    
+    if (!(data->cmdPtr->flags & CMD_IS_DELETED)) {
+	if ((data->cmdPtr->flags & CMD_HAS_EXEC_TRACES) && 
+	    (data->traceCode == TCL_OK)) {
+	    data->traceCode = TclCheckExecutionTraces(interp, 
+		    data->command, data->length,
+		    data->cmdPtr, data->code, TCL_TRACE_LEAVE_EXEC, 
+		    data->objc, data->objv);
+	}
+	if (iPtr->tracePtr != NULL && data->traceCode == TCL_OK) {
+	    data->traceCode = TclCheckInterpTraces(interp, 
+		    data->command, data->length,
+		    data->cmdPtr, data->code, TCL_TRACE_LEAVE_EXEC, 
+		    data->objc, data->objv);
+	}
+    }
+
+    return data->traceCode;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
  * TclEvalObjvInternal --
  *
  *	This function evaluates a Tcl command that has already been parsed
@@ -3234,17 +3555,19 @@
 				 * TCL_EVAL_GLOBAL and TCL_EVAL_INVOKE are
 				 * currently supported. */
 {
-    Command *cmdPtr;
     Interp *iPtr = (Interp *) interp;
-    Tcl_Obj **newObjv;
-    int i;
-    CallFrame *savedVarFramePtr;/* Saves old copy of iPtr->varFramePtr in case
-				 * TCL_EVAL_GLOBAL was set. */
-    int code = TCL_OK;
-    int traceCode = TCL_OK;
-    int checkTraces = 1;
-    int cmdEpoch;
-    Namespace *savedNsPtr = NULL;
+    TclEvalObjvInternalData data; /* State to pass to helper functions */
+
+    data.command            = command;
+    data.length             = length;
+    data.cmdPtr             = NULL;
+    data.objc               = objc;
+    data.objv               = objv;
+    data.code               = TCL_OK;
+    data.traceCode          = TCL_OK;
+    data.flags              = flags;
+    data.savedVarFramePtr   = NULL;
+    data.savedNsPtr         = NULL;
 
     if (TclInterpReady(interp) == TCL_ERROR) {
 	return TCL_ERROR;
@@ -3254,154 +3577,31 @@
 	return TCL_OK;
     }
 
-    /* Configure evaluation context to match the requested flags */
-    savedVarFramePtr = iPtr->varFramePtr;
-    if (flags & TCL_EVAL_GLOBAL) {
-	iPtr->varFramePtr = NULL;
-    } else if ((flags & TCL_EVAL_INVOKE) && iPtr->varFramePtr) {
-	savedNsPtr = iPtr->varFramePtr->nsPtr;
-	iPtr->varFramePtr->nsPtr = iPtr->globalNsPtr;
-    }
-
     /*
-     * Find the function to execute this command. If there isn't one, then see
-     * if there is an unknown command handler registered for this namespace.
-     * If so, create a new word array with the handler as the first words and
-     * the original command words as arguments. Then call ourselves
-     * recursively to execute it.
-     *
-     * If any execution traces rename or delete the current command, we may
-     * need (at most) two passes here.
+     * Lookup command and fire any execution enter traces.
      */
 
-  reparseBecauseOfTraces:
-    cmdPtr = (Command *) Tcl_GetCommandFromObj(interp, objv[0]);
-    if (cmdPtr == NULL) {
-	Namespace *currNsPtr = NULL;	/* Used to check for and invoke any
-					 * registered unknown command handler
-					 * for the current namespace
-					 * (TIP 181). */
-        int newObjc, handlerObjc;
-        Tcl_Obj **handlerObjv;
-
-	if (iPtr->varFramePtr != NULL) {
-	    currNsPtr = iPtr->varFramePtr->nsPtr;
-	}
-	if ((currNsPtr == NULL) || (currNsPtr->unknownHandlerPtr == NULL)) {
-	    currNsPtr = iPtr->globalNsPtr;
-	}
-	if (currNsPtr == NULL) {
-	    Tcl_Panic("TclEvalObjvInternal: NULL global namespace pointer");
-	}
-        if (currNsPtr->unknownHandlerPtr == NULL) {
-            /* Global namespace has lost unknown handler, reset. */
-            currNsPtr->unknownHandlerPtr = Tcl_NewStringObj("::unknown", -1);
-            Tcl_IncrRefCount(currNsPtr->unknownHandlerPtr);
-        }
-        Tcl_ListObjGetElements(NULL, currNsPtr->unknownHandlerPtr,
-		&handlerObjc, &handlerObjv);
-        newObjc = objc + handlerObjc;
-	newObjv = (Tcl_Obj **) ckalloc((unsigned)
-                (newObjc * sizeof(Tcl_Obj *)));
-        /* Copy command prefix from unknown handler. */
-        for (i = 0; i < handlerObjc; ++i) {
-            newObjv[i] = handlerObjv[i];
-	    Tcl_IncrRefCount(newObjv[i]);
-        }
-        /* Add in command name and arguments. */
-        for (i = objc-1; i >= 0; --i) {
-            newObjv[i+handlerObjc] = objv[i];
-        }
-	cmdPtr = (Command *) Tcl_GetCommandFromObj(interp, newObjv[0]);
-	if (cmdPtr == NULL) {
-	    Tcl_AppendResult(interp, "invalid command name \"",
-		    TclGetString(objv[0]), "\"", NULL);
-	    code = TCL_ERROR;
-	} else {
-	    iPtr->numLevels++;
-	    code = TclEvalObjvInternal(interp, newObjc, newObjv, command,
-			    length, 0);
-	    iPtr->numLevels--;
-	}
-        for (i = 0; i < handlerObjc; ++i) {
-	    Tcl_DecrRefCount(newObjv[i]);
-        }
-	ckfree((char *) newObjv);
-	if (savedNsPtr) {
-	    iPtr->varFramePtr->nsPtr = savedNsPtr;
-	}
-	goto done;
-    }
-    if (savedNsPtr) {
-	iPtr->varFramePtr->nsPtr = savedNsPtr;
-    }
-
-    /*
-     * Call trace functions if needed.
-     */
-
-    cmdEpoch = cmdPtr->cmdEpoch;
-    if ((checkTraces) && (command != NULL)) {
-	cmdPtr->refCount++;
-
-	/*
-	 * If the first set of traces modifies/deletes the command or any
-	 * existing traces, then the set checkTraces to 0 and go through this
-	 * while loop one more time.
-	 */
+    TclEvalObjvInternalLookupCommand(interp, &data);
 
-	if (iPtr->tracePtr != NULL && traceCode == TCL_OK) {
-	    traceCode = TclCheckInterpTraces(interp, command, length,
-		    cmdPtr, code, TCL_TRACE_ENTER_EXEC, objc, objv);
-	}
-	if ((cmdPtr->flags & CMD_HAS_EXEC_TRACES) && (traceCode == TCL_OK)) {
-	    traceCode = TclCheckExecutionTraces(interp, command, length,
-		    cmdPtr, code, TCL_TRACE_ENTER_EXEC, objc, objv);
-	}
-	cmdPtr->refCount--;
-    }
-    if (cmdEpoch != cmdPtr->cmdEpoch) {
-	/* The command has been modified in some way. */
-	checkTraces = 0;
-	goto reparseBecauseOfTraces;
+    if (data.cmdPtr == NULL) {
+        /*
+         * Invoked unknown command handler.
+         */
+        goto done;
     }
 
     /*
      * Finally, invoke the command's Tcl_ObjCmdProc.
      */
-
-    cmdPtr->refCount++;
-    iPtr->cmdCount++;
-    if (code == TCL_OK && traceCode == TCL_OK && !Tcl_LimitExceeded(interp)) {
-	if (!(flags & TCL_EVAL_INVOKE) &&
-		(iPtr->ensembleRewrite.sourceObjs != NULL) &&
-		!Tcl_IsEnsemble((Tcl_Command) cmdPtr)) {
-	    iPtr->ensembleRewrite.sourceObjs = NULL;
-	}
-	code = (*cmdPtr->objProc)(cmdPtr->objClientData, interp, objc, objv);
-    }
-    if (Tcl_AsyncReady()) {
-	code = Tcl_AsyncInvoke(interp, code);
-    }
-    if (code == TCL_OK && Tcl_LimitReady(interp)) {
-	code = Tcl_LimitCheck(interp);
-    }
+    
+    TclEvalObjvInternalInvokeCommand(interp, &data);
 
     /*
      * Call 'leave' command traces
      */
 
-    if (!(cmdPtr->flags & CMD_IS_DELETED)) {
-	if ((cmdPtr->flags & CMD_HAS_EXEC_TRACES) && (traceCode == TCL_OK)) {
-	    traceCode = TclCheckExecutionTraces(interp, command, length,
-		    cmdPtr, code, TCL_TRACE_LEAVE_EXEC, objc, objv);
-	}
-	if (iPtr->tracePtr != NULL && traceCode == TCL_OK) {
-	    traceCode = TclCheckInterpTraces(interp, command, length,
-		    cmdPtr, code, TCL_TRACE_LEAVE_EXEC, objc, objv);
-	}
-    }
-    TclCleanupCommand(cmdPtr);
+    TclEvalObjvInternalTraceLeave(interp, &data);
+    TclCleanupCommand(data.cmdPtr);
 
     /*
      * If one of the trace invocation resulted in error, then change the
@@ -3409,8 +3609,8 @@
      * be set correctly by the call to TraceExecutionProc.
      */
 
-    if (traceCode != TCL_OK) {
-	code = traceCode;
+    if (data.traceCode != TCL_OK) {
+	data.code = data.traceCode;
     }
 
     /*
@@ -3425,8 +3625,8 @@
     }
 
     done:
-    iPtr->varFramePtr = savedVarFramePtr;
-    return code;
+    iPtr->varFramePtr = data.savedVarFramePtr;
+    return data.code;
 }
 
 /*