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

  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 Microsoft.JScript.Vsa;
  19.     using System;
  20.     using System.Reflection;
  21.    
  22.     public class ObjectPrototype : JSObject
  23.     {
  24.         static internal readonly ObjectPrototype ob = new ObjectPrototype();
  25.         static internal ObjectConstructor _constructor;
  26.        
  27.         internal ObjectPrototype() : base(null)
  28.         {
  29.             //this.constructor is given a value by the proper constructor class
  30.             if (Globals.contextEngine == null) {
  31.                 this.engine = new VsaEngine(true);
  32.                 //Get here if the class is loaded before an engine is created (ASP+ case).
  33.                 this.engine.InitVsaEngine("JS7://Microsoft.JScript.Vsa.VsaEngine", new DefaultVsaSite());
  34.             }
  35.             else
  36.                 this.engine = Globals.contextEngine;
  37.         }
  38.        
  39.         public static ObjectConstructor constructor {
  40.             get { return ObjectPrototype._constructor; }
  41.         }
  42.        
  43.         static internal ObjectPrototype CommonInstance()
  44.         {
  45.             return ObjectPrototype.ob;
  46.         }
  47.        
  48.         [JSFunctionAttribute(JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_hasOwnProperty)]
  49.         public static bool hasOwnProperty(object thisob, object name)
  50.         {
  51.             string nameStr = Convert.ToString(name);
  52.             if (thisob is ArrayObject) {
  53.                 long index = ArrayObject.Array_index_for(nameStr);
  54.                 if (index >= 0) {
  55.                     object ob = ((ArrayObject)thisob).GetValueAtIndex((uint)index);
  56.                     return ob != null && ob != Missing.Value;
  57.                 }
  58.             }
  59.             if (thisob is JSObject) {
  60.                 MemberInfo[] members = ((JSObject)thisob).GetMember(nameStr, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);
  61.                 int n = members.Length;
  62.                 if (n > 1)
  63.                     return true;
  64.                 if (n < 1)
  65.                     return false;
  66.                 if (members[0] is JSPrototypeField) {
  67.                     //Do not count it as an "own" property unless it has been written to
  68.                     return !(((JSPrototypeField)members[0]).value is Missing);
  69.                 }
  70.                 return true;
  71.             }
  72.             return !(LateBinding.GetMemberValue(thisob, nameStr) is Missing);
  73.         }
  74.        
  75.         [JSFunctionAttribute(JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_isPrototypeOf)]
  76.         public static bool isPrototypeOf(object thisob, object ob)
  77.         {
  78.             if (thisob is ScriptObject && ob is ScriptObject)
  79.                 while (ob != null) {
  80.                     if (ob == thisob)
  81.                         return true;
  82.                     ob = ((ScriptObject)ob).GetParent();
  83.                 }
  84.             return false;
  85.         }
  86.        
  87.         [JSFunctionAttribute(JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_propertyIsEnumerable)]
  88.         public static bool propertyIsEnumerable(object thisob, object name)
  89.         {
  90.             string nameStr = Convert.ToString(name);
  91.             if (thisob is ArrayObject) {
  92.                 long index = ArrayObject.Array_index_for(nameStr);
  93.                 if (index >= 0) {
  94.                     object ob = ((ArrayObject)thisob).GetValueAtIndex((uint)index);
  95.                     return ob != null && ob != Missing.Value;
  96.                 }
  97.             }
  98.             if (thisob is JSObject) {
  99.                 FieldInfo f = ((JSObject)thisob).GetField(nameStr, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);
  100.                 return f != null && f is JSExpandoField;
  101.             }
  102.             return false;
  103.         }
  104.        
  105.         [JSFunctionAttribute(JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_toLocaleString)]
  106.         public static string toLocaleString(object thisob)
  107.         {
  108.             return Convert.ToString(thisob);
  109.         }
  110.        
  111.         [JSFunctionAttribute(JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_toString)]
  112.         public static string toString(object thisob)
  113.         {
  114.             if (thisob is JSObject)
  115.                 return "[object " + ((JSObject)thisob).GetClassName() + "]";
  116.             else
  117.                 return "[object " + thisob.GetType().Name + "]";
  118.         }
  119.        
  120.         [JSFunctionAttribute(JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_valueOf)]
  121.         public static object valueOf(object thisob)
  122.         {
  123.             return thisob;
  124.         }
  125.     }
  126. }

Developer Fusion