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

  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.Diagnostics;
  20.     using System.Globalization;
  21.     using System.Reflection;
  22.     using System.Security.Permissions;
  23.    
  24.     public sealed class Closure : ScriptFunction
  25.     {
  26.         internal FunctionObject func;
  27.         private ScriptObject enclosing_scope;
  28.         private object declaringObject;
  29.        
  30.         public object arguments;
  31.         public object caller;
  32.        
  33.         public Closure(FunctionObject func) : this(func, null)
  34.         {
  35.             if (func.enclosing_scope is StackFrame)
  36.                 this.enclosing_scope = func.enclosing_scope;
  37.         }
  38.        
  39.         internal Closure(FunctionObject func, object declaringObject) : base(func.GetParent(), func.name, func.GetNumberOfFormalParameters())
  40.         {
  41.             this.func = func;
  42.             this.engine = func.engine;
  43.             this.proto = new JSPrototypeObject(((ScriptObject)func.proto).GetParent(), this);
  44.             this.enclosing_scope = this.engine.ScriptObjectStackTop();
  45.             this.arguments = DBNull.Value;
  46.             this.caller = DBNull.Value;
  47.             this.declaringObject = declaringObject;
  48.             this.noExpando = func.noExpando;
  49.             if (func.isExpandoMethod) {
  50.                 StackFrame sf = new StackFrame(new WithObject(this.enclosing_scope, declaringObject), new JSLocalField[0], new object[0], null);
  51.                 this.enclosing_scope = sf;
  52.                 sf.closureInstance = declaringObject;
  53.             }
  54.         }
  55.        
  56.         #if !DEBUG
  57.         [DebuggerStepThroughAttribute()]
  58.         [DebuggerHiddenAttribute()]
  59.         #endif
  60.         internal override object Call(object[] args, object thisob)
  61.         {
  62.             return this.Call(args, thisob, JSBinder.ob, null);
  63.         }
  64.        
  65.         #if !DEBUG
  66.         [DebuggerStepThroughAttribute()]
  67.         [DebuggerHiddenAttribute()]
  68.         #endif
  69.         internal override object Call(object[] args, object thisob, Binder binder, CultureInfo culture)
  70.         {
  71.             if (this.func.isExpandoMethod)
  72.                 ((StackFrame)this.enclosing_scope).thisObject = thisob;
  73.             else if (this.declaringObject != null && !(this.declaringObject is ClassScope))
  74.                 thisob = declaringObject;
  75.             if (thisob == null)
  76.                 thisob = ((IActivationObject)this.engine.ScriptObjectStackTop()).GetDefaultThisObject();
  77.             if (this.enclosing_scope is ClassScope && this.declaringObject == null) {
  78.                 if (thisob is StackFrame)
  79.                     thisob = ((StackFrame)thisob).closureInstance;
  80.                 if (!((FunctionObject)this.func).isStatic && !((ClassScope)(this.enclosing_scope)).HasInstance(thisob))
  81.                     throw new JScriptException(JSError.InvalidCall);
  82.             }
  83.             return this.func.Call(args, thisob, this.enclosing_scope, this, binder, culture);
  84.         }
  85.        
  86.         [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  87.         internal Delegate ConvertToDelegate(Type delegateType)
  88.         {
  89.             return Delegate.CreateDelegate(delegateType, this.declaringObject, this.func.name);
  90.         }
  91.        
  92.         public override string ToString()
  93.         {
  94.             return this.func.ToString();
  95.         }
  96.     }
  97. }

Developer Fusion