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

  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.Reflection;
  20.     using System.Reflection.Emit;
  21.    
  22.     internal sealed class Return : AST
  23.     {
  24.         private Completion completion;
  25.         private AST operand;
  26.         private FunctionScope enclosingFunctionScope;
  27.         private bool leavesFinally;
  28.        
  29.         internal Return(Context context, AST operand, bool leavesFinally) : base(context)
  30.         {
  31.             this.completion = new Completion();
  32.             this.completion.Return = true;
  33.             this.operand = operand;
  34.             ScriptObject scope = Globals.ScopeStack.Peek();
  35.             while (!(scope is FunctionScope)) {
  36.                 scope = scope.GetParent();
  37.                 if (scope == null) {
  38.                     this.context.HandleError(JSError.BadReturn);
  39.                     scope = new FunctionScope(null);
  40.                 }
  41.             }
  42.             this.enclosingFunctionScope = ((FunctionScope)scope);
  43.             if (this.operand != null && this.enclosingFunctionScope.returnVar == null)
  44.                 this.enclosingFunctionScope.AddReturnValueField();
  45.             this.leavesFinally = leavesFinally;
  46.         }
  47.        
  48.         internal override object Evaluate()
  49.         {
  50.             if (this.operand != null)
  51.                 this.completion.value = this.operand.Evaluate();
  52.             return this.completion;
  53.         }
  54.        
  55.         internal override bool HasReturn()
  56.         {
  57.             return true;
  58.         }
  59.        
  60.         internal override AST PartiallyEvaluate()
  61.         {
  62.             if (this.leavesFinally)
  63.                 this.context.HandleError(JSError.BadWayToLeaveFinally);
  64.             if (this.operand != null) {
  65.                 this.operand = this.operand.PartiallyEvaluate();
  66.                 if (this.enclosingFunctionScope.returnVar != null)
  67.                     if (this.enclosingFunctionScope.returnVar.type == null) {
  68.                         this.enclosingFunctionScope.returnVar.SetInferredType(this.operand.InferType(this.enclosingFunctionScope.returnVar), this.operand);
  69.                     }
  70.                     else
  71.                         Binding.AssignmentCompatible(this.enclosingFunctionScope.returnVar.type.ToIReflect(), this.operand, this.operand.InferType(null), true);
  72.                 else {
  73.                     //returning something from a function explicitly typed as Void
  74.                     this.context.HandleError(JSError.CannotReturnValueFromVoidFunction);
  75.                     this.operand = null;
  76.                 }
  77.             }
  78.             else if (this.enclosingFunctionScope.returnVar != null)
  79.                 this.enclosingFunctionScope.returnVar.SetInferredType(Typeob.Object, null);
  80.             return this;
  81.         }
  82.        
  83.         internal override void TranslateToIL(ILGenerator il, Type rtype)
  84.         {
  85.             //This assumes that rtype == Void.class.
  86.             this.context.EmitLineInfo(il);
  87.             if (this.operand != null)
  88.                 this.operand.TranslateToIL(il, this.enclosingFunctionScope.returnVar.FieldType);
  89.             else if (this.enclosingFunctionScope.returnVar != null) {
  90.                 il.Emit(OpCodes.Ldsfld, CompilerGlobals.undefinedField);
  91.                 Convert.Emit(this, il, Typeob.Object, this.enclosingFunctionScope.returnVar.FieldType);
  92.             }
  93.             if (this.enclosingFunctionScope.returnVar != null)
  94.                 il.Emit(OpCodes.Stloc, (LocalBuilder)this.enclosingFunctionScope.returnVar.GetMetaData());
  95.             if (this.leavesFinally) {
  96.                 il.Emit(OpCodes.Newobj, CompilerGlobals.returnOutOfFinallyConstructor);
  97.                 il.Emit(OpCodes.Throw);
  98.             }
  99.             else if (compilerGlobals.InsideProtectedRegion)
  100.                 il.Emit(OpCodes.Leave, this.enclosingFunctionScope.owner.returnLabel);
  101.             else
  102.                 il.Emit(OpCodes.Br, this.enclosingFunctionScope.owner.returnLabel);
  103.         }
  104.        
  105.         internal override void TranslateToILInitializer(ILGenerator il)
  106.         {
  107.             if (this.operand != null)
  108.                 this.operand.TranslateToILInitializer(il);
  109.         }
  110.     }
  111. }

Developer Fusion