override public void Invoke(FakeEnvironment environment) { foreach (var item in ItemsSource) { LoopVariable.SetValue(null, item); var activity = Body(); // Should create a child environment to scope the variable // environment.Variables[LoopVariable.Name] = LoopVariable; activity.Invoke(environment); } }
public override bool Equals(object other) { var otherNode = other as ForLoopNode; if (null == otherNode) { return(false); } return(LoopVariable.Equals(otherNode.LoopVariable) && Expression.Equals(otherNode.Expression) && otherNode != null && Body.SequenceEqual(otherNode.Body)); }
private Expression VisitVariable(ParameterExpression node, ExpressionAccess access) { ParameterExpression box; LoopVariable existing; LocalVariable loc; if (_loopLocals.Contains(node)) { // local to the loop - not propagated in or out return(node); } else if (_loopVariables.TryGetValue(node, out existing)) { // existing outer variable that we are already tracking box = existing.BoxStorage; _loopVariables[node] = new LoopVariable(existing.Access | access, box); } else if (_outerVariables.TryGetValue(node, out loc) || (_closureVariables != null && _closureVariables.TryGetValue(node, out loc))) { // not tracking this variable yet, but defined in outer scope and seen for the 1st time box = loc.InClosureOrBoxed ? Expression.Parameter(typeof(StrongBox <object>), node.Name) : null; _loopVariables[node] = new LoopVariable(access, box); } else { // node is a variable defined in a nested lambda -> skip return(node); } if (box != null) { if ((access & ExpressionAccess.Write) != 0) { // compound assignments were reduced: Debug.Assert((access & ExpressionAccess.Read) == 0); // box.Value = (object)rhs return(LightCompiler.Unbox(box)); } else { // (T)box.Value return(Expression.Convert(LightCompiler.Unbox(box), node.Type)); } } return(node); }
private Expression VisitVariable(ParameterExpression node, ExpressionAccess access) { ParameterExpression box; LoopVariable existing; LocalVariable loc; if (_loopVariables.TryGetValue(node, out existing)) { box = existing.BoxStorage; _loopVariables[node] = new LoopVariable(existing.Access | access, box); } else if (_locals.TryGetLocal(node, out loc)) { box = loc.InClosureOrBoxed ? Expression.Parameter(typeof(StrongBox <object>), node.Name) : null; _loopVariables[node] = new LoopVariable(access, box); } else { // node is a variable defined in a nested lambda -> skip return(node); } if (box != null) { if ((access & ExpressionAccess.Write) != 0) { // compound assignments were reduced: Debug.Assert((access & ExpressionAccess.Read) == 0); // box.Value = (object)rhs return(LightCompiler.Unbox(box)); } else { // (T)box.Value return(Expression.Convert(LightCompiler.Unbox(box), node.Type)); } } return(node); }