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

  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 Conditional : AST
  24.     {
  25.         private AST condition;
  26.         private AST operand1;
  27.         private AST operand2;
  28.        
  29.         internal Conditional(Context context, AST condition, AST operand1, AST operand2) : base(context)
  30.         {
  31.             this.condition = condition;
  32.             this.operand1 = operand1;
  33.             this.operand2 = operand2;
  34.         }
  35.        
  36.         internal override object Evaluate()
  37.         {
  38.             if (Convert.ToBoolean(this.condition.Evaluate()))
  39.                 return this.operand1.Evaluate();
  40.             else
  41.                 return this.operand2.Evaluate();
  42.         }
  43.        
  44.         internal override AST PartiallyEvaluate()
  45.         {
  46.             this.condition = this.condition.PartiallyEvaluate();
  47.             ScriptObject current_scope = Globals.ScopeStack.Peek();
  48.             while (current_scope is WithObject)
  49.                 current_scope = current_scope.GetParent();
  50.             if (current_scope is FunctionScope) {
  51.                 FunctionScope scope = (FunctionScope)current_scope;
  52.                 BitArray before = scope.DefinedFlags;
  53.                 this.operand1 = this.operand1.PartiallyEvaluate();
  54.                 BitArray after1 = scope.DefinedFlags;
  55.                 scope.DefinedFlags = before;
  56.                 this.operand2 = this.operand2.PartiallyEvaluate();
  57.                 BitArray after2 = scope.DefinedFlags;
  58.                 int n = after1.Length;
  59.                 int m = after2.Length;
  60.                 if (n < m)
  61.                     after1.Length = m;
  62.                 if (m < n)
  63.                     after2.Length = n;
  64.                 scope.DefinedFlags = after1.And(after2);
  65.             }
  66.             else {
  67.                 this.operand1 = this.operand1.PartiallyEvaluate();
  68.                 this.operand2 = this.operand2.PartiallyEvaluate();
  69.             }
  70.             return this;
  71.         }
  72.        
  73.         internal override void TranslateToIL(ILGenerator il, Type rtype)
  74.         {
  75.             Label else_label = il.DefineLabel();
  76.             Label endif_label = il.DefineLabel();
  77.             this.condition.TranslateToConditionalBranch(il, false, else_label, false);
  78.             this.operand1.TranslateToIL(il, rtype);
  79.             il.Emit(OpCodes.Br, endif_label);
  80.             il.MarkLabel(else_label);
  81.             this.operand2.TranslateToIL(il, rtype);
  82.             il.MarkLabel(endif_label);
  83.         }
  84.        
  85.         internal override void TranslateToILInitializer(ILGenerator il)
  86.         {
  87.             this.condition.TranslateToILInitializer(il);
  88.             this.operand1.TranslateToILInitializer(il);
  89.             this.operand2.TranslateToILInitializer(il);
  90.         }
  91.     }
  92.    
  93. }

Developer Fusion