5 // Cesar Lopez Nataren (cesar@ciencias.unam.mx)
7 // (C) 2003, Cesar Lopez Nataren
8 // (C) 2005, Novell Inc, (http://novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Diagnostics;
34 using Microsoft.JScript.Vsa;
35 using System.Globalization;
37 namespace Microsoft.JScript {
39 public sealed class Convert {
41 public static bool IsBadIndex (AST ast)
43 throw new NotImplementedException ();
46 internal static bool IsNumberTypeCode (TypeCode tc)
51 case TypeCode.Decimal:
67 internal static bool IsFloatTypeCode (TypeCode tc)
70 case TypeCode.Decimal:
79 public static double CheckIfDoubleIsInteger (double d)
81 if (d == Math.Round (d))
83 throw new NotImplementedException ();
87 public static Single CheckIfSingleIsInteger (Single s)
89 if (s == Math.Round (s))
91 throw new NotImplementedException ();
95 public static object Coerce (object value, object type)
97 throw new NotImplementedException ();
101 public static object CoerceT (object value, Type t, bool explicitOK)
103 throw new NotImplementedException ();
107 public static object Coerce2 (object value, TypeCode target,
108 bool truncationPermitted)
110 throw new NotImplementedException ();
113 [DebuggerStepThroughAttribute]
114 [DebuggerHiddenAttribute]
115 public static void ThrowTypeMismatch (object val)
117 throw new NotImplementedException ();
120 internal static object ToPrimitive (object value, Type hint)
122 IConvertible ic = value as IConvertible;
123 TypeCode tc = Convert.GetTypeCode (value, ic);
125 case TypeCode.Object:
126 if (value is JSObject)
127 return ((JSObject) value).GetDefaultValue (hint);
129 throw new NotImplementedException ();
135 public static bool ToBoolean (double d)
137 return Convert.ToBoolean (d, true);
140 [DebuggerStepThroughAttribute]
141 [DebuggerHiddenAttribute]
142 public static bool ToBoolean (object value)
144 return Convert.ToBoolean (value, true);
147 [DebuggerStepThroughAttribute]
148 [DebuggerHiddenAttribute]
149 public static bool ToBoolean (object value, bool explicitConversion)
151 IConvertible ic = value as IConvertible;
152 TypeCode tc = Convert.GetTypeCode (value, ic);
156 case TypeCode.DBNull:
159 case TypeCode.Boolean:
160 return ic.ToBoolean (null);
165 case TypeCode.UInt16:
166 case TypeCode.UInt32:
169 case TypeCode.Single:
170 case TypeCode.Double:
171 double num = ic.ToDouble (null);
172 return !double.IsNaN (num) && (num != 0.0);
174 case TypeCode.String:
175 string str = ic.ToString (null);
176 return str.Length != 0;
178 case TypeCode.Object:
182 Console.WriteLine ("\nToBoolean: tc = {0}", tc);
185 throw new NotImplementedException ();
188 public static object ToForInObject (object value, VsaEngine engine)
190 throw new NotImplementedException ();
194 public static int ToInt32 (object value)
196 IConvertible ic = value as IConvertible;
197 TypeCode tc = Convert.GetTypeCode (value, ic);
201 case TypeCode.DBNull:
207 case TypeCode.UInt16:
208 case TypeCode.UInt32:
213 case TypeCode.Single:
214 case TypeCode.Double:
215 return (int) Math.Floor ((double) value);
217 case TypeCode.String:
218 return (int) Math.Floor (GlobalObject.parseFloat (ic.ToString ()));
221 Console.WriteLine ("\nToInt32: value.GetType = {0}", value.GetType ());
224 throw new NotImplementedException ();
227 internal static int ToUint16 (object value)
229 double val = Convert.ToNumber (value);
230 if (Double.IsInfinity (val) || double.IsNaN (val))
233 return (int) val % 65536;
236 public static double ToNumber (object value)
238 IConvertible ic = value as IConvertible;
239 TypeCode tc = Convert.GetTypeCode (value, ic);
245 case TypeCode.DBNull:
248 case TypeCode.Boolean:
249 if (ic.ToBoolean (null))
256 case TypeCode.UInt16:
257 case TypeCode.UInt32:
260 return (double) value;
262 case TypeCode.Single:
263 case TypeCode.Double:
264 return (double) value;
266 case TypeCode.String:
267 return GlobalObject.parseFloat (value);
270 Console.WriteLine ("\nToNumber: value.GetType = {0}", value.GetType ());
274 throw new NotImplementedException ();
278 public static double ToNumber (string str)
280 return GlobalObject.parseFloat (str);
284 public static object ToNativeArray (object value, RuntimeTypeHandle handle)
286 throw new NotImplementedException ();
290 public static object ToObject (object value, VsaEngine engine)
292 IConvertible ic = value as IConvertible;
293 TypeCode tc = Convert.GetTypeCode (value, ic);
296 case TypeCode.String:
297 return new StringObject (ic.ToString (null));
298 case TypeCode.Single:
299 case TypeCode.Double:
303 case TypeCode.UInt16:
304 case TypeCode.UInt32:
307 return new NumberObject (ic.ToDouble (null));
308 case TypeCode.Object:
311 Console.WriteLine ("\nToObject: value.GetType = {0}", value.GetType ());
314 throw new NotImplementedException ();
318 public static object ToObject2 (object value, VsaEngine engine)
320 throw new NotImplementedException ();
324 internal static string ToString (object obj)
326 return Convert.ToString (obj, true);
329 [DebuggerStepThroughAttribute]
330 [DebuggerHiddenAttribute]
331 public static string ToString (object value, bool explicitOK)
333 IConvertible ic = value as IConvertible;
334 TypeCode tc = Convert.GetTypeCode (value, ic);
340 case TypeCode.DBNull:
343 case TypeCode.Boolean:
344 bool r = (bool) value;
351 return ic.ToInt16 (null).ToString ();
355 case TypeCode.UInt16:
356 case TypeCode.UInt32:
359 case TypeCode.String:
360 case TypeCode.Double:
361 return ic.ToString (CultureInfo.InvariantCulture);
363 case TypeCode.Object:
364 if (value is ArrayObject)
365 return ArrayPrototype.toString (value);
366 else if (value is BooleanObject)
367 return BooleanPrototype.toString (value);
368 else if (value is DateObject)
369 return DatePrototype.toString (value);
370 else if (value is ErrorObject)
371 return ErrorPrototype.toString (value);
372 else if (value is FunctionObject)
373 return FunctionPrototype.toString (value);
374 else if (value is NumberObject)
375 return NumberPrototype.toString (value, 10);
376 else if (value is ObjectPrototype)
377 return ObjectPrototype.toString (value);
378 else if (value is RegExpObject)
379 return RegExpPrototype.toString (value);
380 else if (value is StringObject)
381 return StringPrototype.toString (value);
382 else if (value is FunctionWrapper)
383 return FunctionPrototype.toString (value);
384 Console.WriteLine ("value.GetType = {0}", value.GetType ());
385 throw new NotImplementedException ();
387 Console.WriteLine ("tc = {0}", tc);
388 throw new NotImplementedException ();
393 public static string ToString (bool b)
395 return b ? "true" : "false";
399 public static string ToString (double d)
401 IConvertible ic = d as IConvertible;
402 return ic.ToString (null);
408 internal static TypeCode GetTypeCode (object obj, IConvertible ic)
411 return TypeCode.Empty;
413 return TypeCode.Object;
415 return ic.GetTypeCode ();