jadams-tresys commented on a change in pull request #450:
URL: https://github.com/apache/incubator-daffodil/pull/450#discussion_r539439407



##########
File path: 
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/VariableMap1.scala
##########
@@ -232,19 +345,33 @@ class VariableMap private(vTable: Map[GlobalQName, 
MStackOf[VariableInstance]])
    */
   def readVariable(vrd: VariableRuntimeData, referringContext: ThrowsSDE, 
maybePstate: Maybe[ParseOrUnparseState]): DataValuePrimitive = {
     val varQName = vrd.globalQName
-    val stack = vTable.get(varQName)
-    if (stack.isDefined) {
-      val variable = stack.get.top
-      variable.state match {
-        case VariableRead if (variable.value.isDefined) => 
variable.value.getNonNullable
-        case VariableDefined | VariableSet if (variable.value.isDefined) => {
+    val abuf = vTable.get(varQName)
+    if (abuf.isDefined) {
+      val variable = abuf.get.last
+      (variable.state, variable.value.isDefined, 
variable.defaultValueExpr.isDefined) match {
+        case (VariableRead, true, _) => variable.value.getNonNullable
+        case (VariableDefined | VariableSet, true, _) => {
           if (maybePstate.isDefined && maybePstate.get.isInstanceOf[PState])
             maybePstate.get.asInstanceOf[PState].markVariableRead(vrd)
 
           variable.setState(VariableRead)
           variable.value.getNonNullable
         }
-        case _ => throw new VariableHasNoValue(varQName, vrd)
+        case (VariableDefined, false, true) => {
+          Assert.invariant(maybePstate.isDefined)
+          variable.setState(VariableInProcess)
+          val pstate = maybePstate.get
+          val res = 
DataValue.unsafeFromAnyRef(variable.defaultValueExpr.get.evaluate(pstate))
+
+          // Need to update the variable's value with the result of the
+          // expression
+          variable.setState(VariableRead)
+          variable.setValue(res)
+
+          res

Review comment:
       So this case here is really only for the case where we have a 
defineVariable with a default value expression that references another variable 
with a default value expression.  So, what happens is when we call the 
forceExpressoinEvaluations at the start of parsing, it evaluates the top level 
variable default value expression, which then calls readVariable for the 
variable that it references, which then needs it's expression evaluated at that 
time.  I'll add a comment to this case explaining what is happening when this 
case is hit.
   
   This isn't an issue for newVariableInstance, as we do no need to postpone 
the evaluation of expressions because the VariableMap alreadys exists by the 
time we would run into any newVariableInstance expressions.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to