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

  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 While : AST
  24.     {
  25.         private AST condition;
  26.         private AST body;
  27.         private Completion completion;
  28.        
  29.         internal While(Context context, AST condition, AST body) : base(context)
  30.         {
  31.             this.condition = condition;
  32.             this.body = body;
  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.             while (Convert.ToBoolean(this.condition.Evaluate()) == true) {
  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.             return this.completion;
  57.         }
  58.        
  59.         internal override AST PartiallyEvaluate()
  60.         {
  61.             this.condition = this.condition.PartiallyEvaluate();
  62.            
  63.             IReflect conditiontype = this.condition.InferType(null);
  64.             if (conditiontype is FunctionPrototype || conditiontype == Typeob.ScriptFunction)
  65.                 this.context.HandleError(JSError.SuspectLoopCondition);
  66.            
  67.             ScriptObject current_scope = Globals.ScopeStack.Peek();
  68.             while (current_scope is WithObject)
  69.                 current_scope = current_scope.GetParent();
  70.             if (current_scope is FunctionScope) {
  71.                 FunctionScope scope = (FunctionScope)current_scope;
  72.                 BitArray before = scope.DefinedFlags;
  73.                 this.body = this.body.PartiallyEvaluate();
  74.                 scope.DefinedFlags = before;
  75.             }
  76.             else
  77.                 this.body = this.body.PartiallyEvaluate();
  78.             return this;
  79.         }
  80.        
  81.         internal override void TranslateToIL(ILGenerator il, Type rtype)
  82.         {
  83.             //This assumes rtype == Void.class
  84.             Label loop_start = il.DefineLabel();
  85.             Label loop_end = il.DefineLabel();
  86.             Label body = il.DefineLabel();
  87.             compilerGlobals.BreakLabelStack.Push(loop_end);
  88.             compilerGlobals.ContinueLabelStack.Push(loop_start);
  89.             il.Emit(OpCodes.Br, loop_start);
  90.             il.MarkLabel(body);
  91.             this.body.TranslateToIL(il, Typeob.Void);
  92.             il.MarkLabel(loop_start);
  93.             this.context.EmitLineInfo(il);
  94.             this.condition.TranslateToConditionalBranch(il, true, body, false);
  95.             il.MarkLabel(loop_end);
  96.            
  97.             compilerGlobals.BreakLabelStack.Pop();
  98.             compilerGlobals.ContinueLabelStack.Pop();
  99.         }
  100.        
  101.         internal override void TranslateToILInitializer(ILGenerator il)
  102.         {
  103.             this.condition.TranslateToILInitializer(il);
  104.             this.body.TranslateToILInitializer(il);
  105.         }
  106.     }
  107. }

Developer Fusion