merge from trunk revisions 58933, 58935, 58936
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / ObjectPrototype.cs
1 //
2 // ObjectPrototype.cs
3 //
4 // Author:
5 //      Cesar Lopez Nataren (cesar@ciencias.unam.mx)
6 //
7 // (C) 2003, Cesar Lopez Nataren
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Reflection;
33
34 namespace Microsoft.JScript {
35
36         public class ObjectPrototype : JSObject {
37
38                 internal ObjectPrototype ()
39                 {
40                 }
41
42                 internal static ObjectPrototype Proto = new ObjectPrototype ();
43
44                 public static ObjectConstructor constructor {
45                         get { return ObjectConstructor.Ctr; }
46                 }
47
48                 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_hasOwnProperty)]
49                 public static bool hasOwnProperty (object thisObj, object name)
50                 {
51                         ScriptObject obj = thisObj as ScriptObject;
52                         if (obj == null)
53                                 return false;
54                         string key = Convert.ToString (name);
55                         return LateBinding.DirectHasObjectProperty (obj, key);
56                 }
57
58                 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_isPrototypeOf)]
59                 public static bool isPrototypeOf (object thisObj, object obj)
60                 {
61                         throw new NotImplementedException ();
62                 }
63
64                 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_propertyIsEnumerable)]
65                 public static bool propertyIsEnumerable (object thisObj, object name)
66                 {
67                         if (thisObj == null || name == null)
68                                 return false;
69                         // TODO: Implement me.
70                         // Type type = thisObj.GetType ();
71                         // FieldInfo res = type.GetField (Convert.ToString (name));
72                         return true;
73                 }
74
75                 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_toLocaleString)]
76                 public static string toLocaleString (object thisObj)
77                 {
78                         return toString (thisObj);
79                 }
80
81                 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_toString)]
82                 public static string toString (object thisObj)
83                 {
84                         if (thisObj is ScriptObject) {
85                                 ScriptObject obj = (ScriptObject) thisObj;
86                                 return "[object " + obj.ClassName + "]";
87                         } else
88                                 throw new NotImplementedException ();
89                 }
90
91                 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Object_valueOf)]
92                 public static object valueOf (object thisObj)
93                 {
94                         return thisObj;
95                 }
96
97                 internal static object smartToString (JSObject thisObj)
98                 {
99                         JSObject obj = (JSObject) thisObj;
100                         object val = obj.GetDefaultValue (typeof (string), true);
101                         if (val == thisObj)
102                                 return toString (thisObj);
103                         else
104                                 return Convert.ToString (val);
105                 }
106         }
107 }