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

  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.Text;
  23.     using System.Security.Permissions;
  24.    
  25.     public sealed class FunctionWrapper : ScriptFunction
  26.     {
  27.         private object obj;
  28.         private MemberInfo[] members;
  29.        
  30.         internal FunctionWrapper(string name, object obj, MemberInfo[] members) : base(FunctionPrototype.ob, name, 0)
  31.         {
  32.             this.obj = obj;
  33.             this.members = members;
  34.             Debug.Assert(members.Length > 0);
  35.             foreach (MemberInfo mem in members) {
  36.                 if (mem is MethodInfo) {
  37.                     this.ilength = ((MethodInfo)mem).GetParameters().Length;
  38.                     break;
  39.                 }
  40.             }
  41.         }
  42.        
  43.         #if !DEBUG
  44.         [DebuggerStepThroughAttribute()]
  45.         [DebuggerHiddenAttribute()]
  46.         #endif
  47.         internal override object Call(object[] args, object thisob)
  48.         {
  49.             return this.Call(args, thisob, null, null);
  50.         }
  51.        
  52.         #if !DEBUG
  53.         [DebuggerStepThroughAttribute()]
  54.         [DebuggerHiddenAttribute()]
  55.         #endif
  56.         internal override object Call(object[] args, object thisob, Binder binder, CultureInfo culture)
  57.         {
  58.             MethodInfo m = this.members[0] as MethodInfo;
  59.             if (thisob is GlobalScope || thisob == null || m != null && (m.Attributes & MethodAttributes.Static) != 0)
  60.                 thisob = this.obj;
  61.             else if ((!this.obj.GetType().IsInstanceOfType(thisob) && !(this.obj is ClassScope))) {
  62.                 if (this.members.Length == 1) {
  63.                     JSWrappedMethod wm = this.members[0] as JSWrappedMethod;
  64.                     if (wm != null && wm.DeclaringType == Typeob.Object)
  65.                         return LateBinding.CallOneOfTheMembers(new MemberInfo[] {wm.method}, args, false, thisob, binder, culture, null, this.engine);
  66.                 }
  67.                 throw new JScriptException(JSError.TypeMismatch);
  68.             }
  69.             return LateBinding.CallOneOfTheMembers(this.members, args, false, thisob, binder, culture, null, this.engine);
  70.         }
  71.        
  72.         [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  73.         internal Delegate ConvertToDelegate(Type delegateType)
  74.         {
  75.             return Delegate.CreateDelegate(delegateType, this.obj, this.name);
  76.         }
  77.        
  78.         public override string ToString()
  79.         {
  80.             Type t = this.members[0].DeclaringType;
  81.             MethodInfo meth = (t == null ? null : t.GetMethod(this.name + " source"));
  82.             if (meth == null) {
  83.                 StringBuilder result = new StringBuilder();
  84.                 bool firstTime = true;
  85.                 foreach (MemberInfo mem in this.members)
  86.                     if (mem is MethodInfo || (mem is PropertyInfo && JSProperty.GetGetMethod((PropertyInfo)mem, false) != null)) {
  87.                         if (!firstTime)
  88.                             result.Append("\n");
  89.                         else
  90.                             firstTime = false;
  91.                         result.Append(mem.ToString());
  92.                     }
  93.                 if (result.Length > 0)
  94.                     return result.ToString();
  95.                 return "function " + this.name + "() {\n [native code]\n}";
  96.             }
  97.             return (string)meth.Invoke(null, null);
  98.         }
  99.     }
  100. }

Developer Fusion