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

  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.     using System.Globalization;
  22.     using System.Diagnostics;
  23.    
  24.     internal sealed class JSFieldMethod : JSMethod
  25.     {
  26.         internal FieldInfo field;
  27.         internal FunctionObject func;
  28.        
  29.         private static readonly ParameterInfo[] EmptyParams = new ParameterInfo[0];
  30.        
  31.         internal JSFieldMethod(FieldInfo field, object obj) : base(obj)
  32.         {
  33.             //The object is never used, but it is convenient to have the field on JSMethod.
  34.             this.field = field;
  35.             this.func = null;
  36.             if (!field.IsLiteral)
  37.                 return;
  38.             //If we get here, we know at compile time what function we are calling via the field. I.e. we're in fast mode.
  39.             object val = field is JSVariableField ? ((JSVariableField)field).value : field.GetValue(null);
  40.             if (val is FunctionObject)
  41.                 this.func = (FunctionObject)val;
  42.         }
  43.        
  44.         internal override object Construct(object[] args)
  45.         {
  46.             return LateBinding.CallValue(this.field.GetValue(this.obj), args, true, false, ((ScriptObject)this.obj).engine, null, JSBinder.ob, null, null);
  47.         }
  48.        
  49.         public override MethodAttributes Attributes {
  50.             get {
  51.                 if (this.func != null)
  52.                     return this.func.attributes;
  53.                 else if (field.IsPublic)
  54.                     return MethodAttributes.Public;
  55.                 else if (field.IsFamily)
  56.                     return MethodAttributes.Family;
  57.                 else if (field.IsAssembly)
  58.                     return MethodAttributes.Assembly;
  59.                 else
  60.                     return MethodAttributes.Private;
  61.             }
  62.         }
  63.        
  64.         public override Type DeclaringType {
  65.             get {
  66.                 if (this.func != null)
  67.                     return Convert.ToType(this.func.enclosing_scope);
  68.                 else
  69.                     return Typeob.Object;
  70.             }
  71.         }
  72.        
  73.         internal ScriptObject EnclosingScope()
  74.         {
  75.             if (this.func != null)
  76.                 return this.func.enclosing_scope;
  77.             else
  78.                 return null;
  79.         }
  80.        
  81.         public override object[] GetCustomAttributes(bool inherit)
  82.         {
  83.             if (this.func != null) {
  84.                 CustomAttributeList caList = this.func.customAttributes;
  85.                 if (caList != null)
  86.                     return (object[])caList.Evaluate(inherit);
  87.             }
  88.             return new object[0];
  89.         }
  90.        
  91.         public override ParameterInfo[] GetParameters()
  92.         {
  93.             if (this.func != null)
  94.                 return this.func.parameter_declarations;
  95.             else
  96.                 return JSFieldMethod.EmptyParams;
  97.         }
  98.        
  99.         internal override MethodInfo GetMethodInfo(CompilerGlobals compilerGlobals)
  100.         {
  101.             return this.func.GetMethodInfo(compilerGlobals);
  102.         }
  103.        
  104.         #if !DEBUG
  105.         [DebuggerStepThroughAttribute()]
  106.         [DebuggerHiddenAttribute()]
  107.         #endif
  108.         internal override object Invoke(object obj, object thisob, BindingFlags options, Binder binder, object[] parameters, CultureInfo culture)
  109.         {
  110.             bool construct = (options & BindingFlags.CreateInstance) != 0;
  111.             bool brackets = (options & BindingFlags.GetProperty) != 0 && (options & BindingFlags.InvokeMethod) == 0;
  112.             object f = this.func;
  113.             if (f == null)
  114.                 f = this.field.GetValue(this.obj);
  115.             FunctionObject func = f as FunctionObject;
  116.             JSObject jsOb = obj as JSObject;
  117.             if (jsOb != null && func != null && func.isMethod && (func.attributes & MethodAttributes.Virtual) != 0 && jsOb.GetParent() != func.enclosing_scope && ((ClassScope)func.enclosing_scope).HasInstance(jsOb)) {
  118.                 LateBinding lb = new LateBinding(func.name);
  119.                 lb.obj = jsOb;
  120.                 return lb.Call(parameters, construct, brackets, ((ScriptObject)this.obj).engine);
  121.             }
  122.             return LateBinding.CallValue(f, parameters, construct, brackets, ((ScriptObject)this.obj).engine, thisob, binder, culture, null);
  123.         }
  124.        
  125.         internal bool IsAccessibleFrom(ScriptObject scope)
  126.         {
  127.             return ((JSMemberField)this.field).IsAccessibleFrom(scope);
  128.         }
  129.        
  130.         public override string Name {
  131.             get { return this.field.Name; }
  132.         }
  133.        
  134.         public override Type ReturnType {
  135.             get {
  136.                 if (this.func != null)
  137.                     return Convert.ToType(this.func.ReturnType(null));
  138.                 else
  139.                     return Typeob.Object;
  140.             }
  141.         }
  142.        
  143.         internal IReflect ReturnIR()
  144.         {
  145.             if (this.func != null)
  146.                 return this.func.ReturnType(null);
  147.             else
  148.                 return Typeob.Object;
  149.         }
  150.     }
  151. }

Developer Fusion