Subject: New internal routine TclFetchLiteral() for better CompileEnv encapsulation.

From: dgp <[email-masked]>
Date: Tue, 5 Mar 2013 22:13:30 +0000

---
 generic/tclCompCmds.c |  3 +--
 generic/tclCompExpr.c | 13 +++++--------
 generic/tclCompile.c  | 18 +++++++++---------
 generic/tclCompile.h  |  1 +
 generic/tclEnsemble.c |  6 +++---
 generic/tclLiteral.c  | 27 +++++++++++++++++++++++++++
 6 files changed, 46 insertions(+), 22 deletions(-)

diff --git a/generic/tclCompCmds.c b/generic/tclCompCmds.c
index 4751455..40348fa 100644
--- a/generic/tclCompCmds.c
+++ b/generic/tclCompCmds.c
@@ -349,8 +349,7 @@ TclCompileArraySetCmd(
 	Tcl_GetCommandFullName(interp, (Tcl_Command) cmdPtr, objPtr);
 	bytes = Tcl_GetStringFromObj(objPtr, &length);
 	cmdLit = TclRegisterNewCmdLiteral(envPtr, bytes, length);
-	TclSetCmdNameObj(interp, envPtr->literalArrayPtr[cmdLit].objPtr,
-		cmdPtr);
+	TclSetCmdNameObj(interp, TclFetchLiteral(envPtr, cmdLit), cmdPtr);
 	TclEmitPush(cmdLit, envPtr);
 	TclDecrRefCount(objPtr);
 	if (localIndex >= 0) {
diff --git a/generic/tclCompExpr.c b/generic/tclCompExpr.c
index 346f446..3597abe 100644
--- a/generic/tclCompExpr.c
+++ b/generic/tclCompExpr.c
@@ -2445,14 +2445,11 @@ CompileExprTree(
 	    Tcl_Obj *literal = *litObjv;
 
 	    if (optimize) {
-		int length, index;
+		int length;
 		const char *bytes = TclGetStringFromObj(literal, &length);
-		LiteralEntry *lePtr;
-		Tcl_Obj *objPtr;
-
-		index = TclRegisterNewLiteral(envPtr, bytes, length);
-		lePtr = envPtr->literalArrayPtr + index;
-		objPtr = lePtr->objPtr;
+		int index = TclRegisterNewLiteral(envPtr, bytes, length);
+		Tcl_Obj *objPtr = TclFetchLiteral(envPtr, index);
+		
 		if ((objPtr->typePtr == NULL) && (literal->typePtr != NULL)) {
 		    /*
 		     * Would like to do this:
@@ -2511,7 +2508,7 @@ CompileExprTree(
 
 			index = TclRegisterNewLiteral(envPtr, objPtr->bytes,
 				objPtr->length);
-			tableValue = envPtr->literalArrayPtr[index].objPtr;
+			tableValue = TclFetchLiteral(envPtr, index);
 			if ((tableValue->typePtr == NULL) &&
 				(objPtr->typePtr != NULL)) {
 			    /*
diff --git a/generic/tclCompile.c b/generic/tclCompile.c
index cf1e25e..5427759 100644
--- a/generic/tclCompile.c
+++ b/generic/tclCompile.c
@@ -1896,8 +1896,7 @@ TclCompileScript(
 			    tokenPtr[1].start, tokenPtr[1].size);
 		    if (cmdPtr != NULL) {
 			TclSetCmdNameObj(interp,
-				envPtr->literalArrayPtr[objIndex].objPtr,
-				cmdPtr);
+				TclFetchLiteral(envPtr, objIndex), cmdPtr);
 		    }
 		} else {
 		    /*
@@ -1914,7 +1913,7 @@ TclCompileScript(
 
 		    if (envPtr->clNext) {
 			TclContinuationsEnterDerived(
-				envPtr->literalArrayPtr[objIndex].objPtr,
+				TclFetchLiteral(envPtr, objIndex),
 				tokenPtr[1].start - envPtr->source,
 				eclPtr->loc[wlineat].next[wordIdx]);
 		    }
@@ -2223,9 +2222,8 @@ TclCompileTokens(
 		Tcl_DStringFree(&textBuffer);
 
 		if (numCL) {
-		    TclContinuationsEnter(
-			    envPtr->literalArrayPtr[literal].objPtr, numCL,
-			    clPosition);
+		    TclContinuationsEnter(TclFetchLiteral(envPtr, literal),
+			    numCL, clPosition);
 		}
 		numCL = 0;
 	    }
@@ -2271,7 +2269,7 @@ TclCompileTokens(
 	TclEmitPush(literal, envPtr);
 	numObjsToConcat++;
 	if (numCL) {
-	    TclContinuationsEnter(envPtr->literalArrayPtr[literal].objPtr,
+	    TclContinuationsEnter(TclFetchLiteral(envPtr, literal),
 		    numCL, clPosition);
 	}
 	numCL = 0;
@@ -2579,7 +2577,9 @@ TclInitByteCodeObj(
     p += TCL_ALIGN(codeBytes);		/* align object array */
     codePtr->objArrayPtr = (Tcl_Obj **) p;
     for (i = 0;  i < numLitObjects;  i++) {
-	if (objPtr == envPtr->literalArrayPtr[i].objPtr) {
+	Tcl_Obj *fetched = TclFetchLiteral(envPtr, i);
+
+	if (objPtr == fetched) {
 	    /*
 	     * Prevent circular reference where the bytecode intrep of
 	     * a value contains a literal which is that same value.
@@ -2598,7 +2598,7 @@ TclInitByteCodeObj(
 	    Tcl_IncrRefCount(codePtr->objArrayPtr[i]);
 	    Tcl_DecrRefCount(objPtr);
 	} else {
-	    codePtr->objArrayPtr[i] = envPtr->literalArrayPtr[i].objPtr;
+	    codePtr->objArrayPtr[i] = fetched;
 	}
     }
 
diff --git a/generic/tclCompile.h b/generic/tclCompile.h
index 10282ba..79497d2 100644
--- a/generic/tclCompile.h
+++ b/generic/tclCompile.h
@@ -954,6 +954,7 @@ MODULE_SCOPE ExceptionRange * TclGetExceptionRangeForPc(unsigned char *pc,
 MODULE_SCOPE void	TclExpandJumpFixupArray(JumpFixupArray *fixupArrayPtr);
 MODULE_SCOPE int	TclNRExecuteByteCode(Tcl_Interp *interp,
 			    ByteCode *codePtr);
+MODULE_SCOPE Tcl_Obj *	TclFetchLiteral(CompileEnv *envPtr, unsigned int index);
 MODULE_SCOPE void	TclFinalizeAuxDataTypeTable(void);
 MODULE_SCOPE int	TclFindCompiledLocal(const char *name, int nameChars,
 			    int create, CompileEnv *envPtr);
diff --git a/generic/tclEnsemble.c b/generic/tclEnsemble.c
index bf9dac2..813e056 100644
--- a/generic/tclEnsemble.c
+++ b/generic/tclEnsemble.c
@@ -3167,7 +3167,7 @@ CompileToInvokedCommand(
 
 	    if (envPtr->clNext) {
 		TclContinuationsEnterDerived(
-			envPtr->literalArrayPtr[literal].objPtr,
+			TclFetchLiteral(envPtr, literal),
 			tokPtr[1].start - envPtr->source,
 			mapPtr->loc[eclIndex].next[i]);
 	    }
@@ -3190,7 +3190,7 @@ CompileToInvokedCommand(
     Tcl_GetCommandFullName(interp, (Tcl_Command) cmdPtr, objPtr);
     bytes = Tcl_GetStringFromObj(objPtr, &length);
     cmdLit = TclRegisterNewCmdLiteral(envPtr, bytes, length);
-    TclSetCmdNameObj(interp, envPtr->literalArrayPtr[cmdLit].objPtr, cmdPtr);
+    TclSetCmdNameObj(interp, TclFetchLiteral(envPtr, cmdLit), cmdPtr);
     TclEmitPush(cmdLit, envPtr);
     TclDecrRefCount(objPtr);
 
@@ -3239,7 +3239,7 @@ CompileBasicNArgCommand(
     Tcl_GetCommandFullName(interp, (Tcl_Command) cmdPtr, objPtr);
     bytes = Tcl_GetStringFromObj(objPtr, &length);
     literal = TclRegisterNewCmdLiteral(envPtr, bytes, length);
-    TclSetCmdNameObj(interp, envPtr->literalArrayPtr[literal].objPtr, cmdPtr);
+    TclSetCmdNameObj(interp, TclFetchLiteral(envPtr, literal), cmdPtr);
     TclEmitPush(literal, envPtr);
     TclDecrRefCount(objPtr);
 
diff --git a/generic/tclLiteral.c b/generic/tclLiteral.c
index bd5fe73..e2ee9b4 100644
--- a/generic/tclLiteral.c
+++ b/generic/tclLiteral.c
@@ -305,6 +305,33 @@ TclCreateLiteral(
 /*
  *----------------------------------------------------------------------
  *
+ * TclFetchLiteral --
+ *
+ *	Fetch from a CompileEnv the literal value identified by an index
+ *	value, as returned by a prior call to TclRegisterLiteral().
+ *
+ * Results:
+ *	The literal value, or NULL if the index is out of range.
+ *
+ *----------------------------------------------------------------------
+ */
+
+Tcl_Obj *
+TclFetchLiteral(
+    CompileEnv *envPtr,		/* Points to the CompileEnv from which to
+				 * fetch the registered literal value. */
+    unsigned int index)		/* Index of the desired literal, as returned
+				 * by prior call to TclRegisterLiteral() */
+{
+    if (index >= envPtr->literalArrayNext) {
+	return NULL;
+    }
+    return envPtr->literalArrayPtr[index].objPtr;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
  * TclRegisterLiteral --
  *
  *	Find, or if necessary create, an object in a CompileEnv literal array
-- 
cgit v0.12