The Labs \ Source Viewer \ SSCLI \ Microsoft.JScript \ DoWhile

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace Microsoft.JScript
  16. {
  17.    
  18.     using System;
  19.     using System.Collections;
  20.     using System.Reflection;
  21.     using System.Reflection.Emit;
  22.    
  23.     internal sealed class DoWhile : AST
  24.     {
  25.         private AST body;
  26.         private AST condition;
  27.         private Completion completion;
  28.        
  29.         internal DoWhile(Context context, AST body, AST condition) : base(context)
  30.         {
  31.             this.body = body;
  32.             this.condition = condition;
  33.             this.completion = new Completion();
  34.         }
  35.        
  36.         internal override object Evaluate()
  37.         {
  38.             this.completion.Continue = 0;
  39.             this.completion.Exit = 0;
  40.             this.completion.value = null;
  41.             do {
  42.                 Completion c = (Completion)this.body.Evaluate();
  43.                 if (c.value != null)
  44.                     this.completion.value = c.value;
  45.                 if (c.Continue > 1) {
  46.                     this.completion.Continue = c.Continue - 1;
  47.                     break;
  48.                 }
  49.                 if (c.Exit > 0) {
  50.                     this.completion.Exit = c.Exit - 1;
  51.                     break;
  52.                 }
  53.                 if (c.Return)
  54.                     return c;
  55.             }
  56.             while (Convert.ToBoolean(this.condition.Evaluate()) == true);
  57.             return this.completion;
  58.         }
  59.        
  60.         internal override AST PartiallyEvaluate()
  61.         {
  62.             ScriptObject current_scope = Globals.ScopeStack.Peek();
  63.             while (current_scope is WithObject)
  64.                 current_scope = current_scope.GetParent();
  65.             if (current_scope is FunctionScope) {
  66.                 FunctionScope scope = (FunctionScope)current_scope;
  67.                 BitArray before = scope.DefinedFlags;
  68.                 this.body = this.body.PartiallyEvaluate();
  69.                 scope.DefinedFlags = before;
  70.                 //The body may have a continue in it
  71.                 this.condition = this.condition.PartiallyEvaluate();
  72.                 scope.DefinedFlags = before;
  73.                 //This is very pessimistic, but the potential presence of break/continue/return/throw
  74.                 //inside the loop presents complications that are hard to resolve naively.
  75.             }
  76.             else {
  77.                 this.body = this.body.PartiallyEvaluate();
  78.                 this.condition = this.condition.PartiallyEvaluate();
  79.             }
  80.             IReflect conditiontype = this.condition.InferType(null);
  81.             if (conditiontype is FunctionPrototype || conditiontype == Typeob.ScriptFunction)
  82.                 this.context.HandleError(JSError.SuspectLoopCondition);
  83.             return this;
  84.         }
  85.        
  86.         internal override void TranslateToIL(ILGenerator il, Type rtype)
  87.         {
  88.             //This assumes that rtype == Void.class
  89.             Label loop_start = il.DefineLabel();
  90.             Label condition_start = il.DefineLabel();
  91.             Label loop_end = il.DefineLabel();
  92.             compilerGlobals.BreakLabelStack.Push(loop_end);
  93.             compilerGlobals.ContinueLabelStack.Push(condition_start);
  94.             il.MarkLabel(loop_start);
  95.             this.body.TranslateToIL(il, Typeob.Void);
  96.             il.MarkLabel(condition_start);
  97.             this.context.EmitLineInfo(il);
  98.             this.condition.TranslateToConditionalBranch(il, true, loop_start, false);
  99.             il.MarkLabel(loop_end);
  100.             compilerGlobals.BreakLabelStack.Pop();
  101.             compilerGlobals.ContinueLabelStack.Pop();
  102.         }
  103.        
  104.         internal override void TranslateToILInitializer(ILGenerator il)
  105.         {
  106.             this.body.TranslateToILInitializer(il);
  107.             this.condition.TranslateToILInitializer(il);
  108.         }
  109.        
  110.         internal override Context GetFirstExecutableContext()
  111.         {
  112.             return this.body.GetFirstExecutableContext();
  113.         }
  114.        
  115.     }
  116.    
  117. }

Developer Fusion