2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / Convert.cs
1 //
2 // Convert.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 Microsoft.JScript.Vsa;
33
34 namespace Microsoft.JScript {
35
36         public sealed class Convert {
37
38                 public static bool IsBadIndex (AST ast)
39                 {
40                         throw new NotImplementedException ();
41                 }
42
43
44                 public static double CheckIfDoubleIsInteger (double d)
45                 {
46                         if (d == Math.Round (d))
47                                 return d;
48                         throw new NotImplementedException ();
49                 }
50
51
52                 public static Single CheckIfSingleIsInteger (Single s)
53                 {
54                         if (s == Math.Round (s))
55                                 return s;
56                         throw new NotImplementedException ();
57                 }
58
59
60                 public static object Coerce (object value, object type)
61                 {
62                         throw new NotImplementedException ();
63                 }
64
65
66                 public static object CoerceT (object value, Type t, bool explicitOK)
67                 {
68                         throw new NotImplementedException ();
69                 }
70
71
72                 public static object Coerce2 (object value, TypeCode target, 
73                                               bool truncationPermitted)
74                 {
75                         throw new NotImplementedException ();
76                 }
77
78                 public static void ThrowTypeMismatch (object val)
79                 {
80                         throw new NotImplementedException ();
81                 }
82
83
84                 public static bool ToBoolean (double d)
85                 {
86                         throw new NotImplementedException ();
87                 }
88
89
90                 public static bool ToBoolean (object value)
91                 {
92                         throw new NotImplementedException ();
93                 }
94
95
96                 public static bool ToBoolean (object value, bool explicitConversion)
97                 {
98                         return false;
99                 }
100
101
102                 public static object ToForInObject (object value, VsaEngine engine)
103                 {
104                         throw new NotImplementedException ();
105                 }
106
107
108                 public static int ToInt32 (object value)
109                 {
110                         throw new NotImplementedException ();
111                 }
112
113
114                 public static double ToNumber (object value)
115                 {
116                         throw new NotImplementedException ();
117                 }
118
119
120                 public static double ToNumber (string str)
121                 {
122                         throw new NotImplementedException ();
123                 }
124
125
126                 public static object ToNativeArray (object value, RuntimeTypeHandle handle)
127                 {
128                         throw new NotImplementedException ();
129                 }
130
131
132                 public static object ToObject (object value, VsaEngine engine)
133                 {
134                         throw new NotImplementedException ();
135                 }
136
137
138                 public static object ToObject2 (object value, VsaEngine engine)
139                 {
140                         throw new NotImplementedException ();
141                 }
142
143
144                 internal static string ToString (object obj)
145                 {
146                         return Convert.ToString (obj, true);
147                 }
148
149                 public static string ToString (object value, bool explicitOK)
150                 {
151                         IConvertible ic = value as IConvertible;
152                         TypeCode tc = Convert.GetTypeCode (value, ic);
153
154                         switch (tc) {
155                         case TypeCode.DBNull:
156                                 return "null";
157
158                         case TypeCode.String:
159                         case TypeCode.Double:
160                                 return ic.ToString (null);
161
162                         case TypeCode.Object:
163                                 if (value is ArrayObject)
164                                         return ArrayPrototype.toString (value);
165                                 else if (value is BooleanObject)
166                                         return BooleanPrototype.toString (value);
167                                 else if (value is DateObject)
168                                         return DatePrototype.toString (value);
169                                 else if (value is ErrorObject)
170                                         return ErrorPrototype.toString (value);
171                                 else if (value is FunctionObject)
172                                         return FunctionPrototype.toString (value);
173                                 else if (value is NumberObject)
174                                         return NumberPrototype.toString (value, 10);
175                                 else if (value is ObjectPrototype)
176                                         return ObjectPrototype.toString (value);
177                                 else if (value is RegExpObject)
178                                         return RegExpPrototype.toString (value);
179                                 else if (value is StringObject)
180                                         return StringPrototype.toString (value);
181                                 throw new NotImplementedException ();
182                         default:
183                                 throw new NotImplementedException ();
184                         }
185                 }
186
187
188                 public static string ToString (bool b)
189                 {
190                         return b ? "true" : "false";
191                 }
192
193
194                 public static string ToString (double d)
195                 {       
196                         IConvertible ic = d as IConvertible;
197                         return ic.ToString (null);
198                 }
199
200                 //
201                 // Utility methods
202                 //
203                 internal static TypeCode GetTypeCode (object obj, IConvertible ic)
204                 {
205                         if (obj == null)
206                                 return TypeCode.Empty;
207                         else if (ic == null)
208                                 return TypeCode.Object;
209                         else 
210                                 return  ic.GetTypeCode ();
211                 }
212         }
213 }