TypeConverter.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using Jint.Native;
  3. using Jint.Native.Errors;
  4. using Jint.Native.Object;
  5. namespace Jint.Runtime
  6. {
  7. public class TypeConverter
  8. {
  9. /// <summary>
  10. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.1
  11. /// </summary>
  12. /// <param name="input"></param>
  13. /// <param name="preferredType"></param>
  14. /// <returns></returns>
  15. public static object ToPrimitive(object input, TypeCode preferredType)
  16. {
  17. return Undefined.Instance;
  18. }
  19. /// <summary>
  20. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
  21. /// </summary>
  22. /// <param name="o"></param>
  23. /// <returns></returns>
  24. public static bool ToBoolean(object o)
  25. {
  26. return (bool) o;
  27. }
  28. /// <summary>
  29. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.3
  30. /// </summary>
  31. /// <param name="o"></param>
  32. /// <returns></returns>
  33. public static double ToNumber(object o)
  34. {
  35. if (o == Undefined.Instance)
  36. {
  37. return double.NaN;
  38. }
  39. if (o == Null.Instance)
  40. {
  41. return 0;
  42. }
  43. if (o is bool)
  44. {
  45. return (bool)o ? 1 : 0;
  46. }
  47. if (o is double)
  48. {
  49. return (double)o;
  50. }
  51. var s = o as string;
  52. if (s != null)
  53. {
  54. return double.Parse(s);
  55. }
  56. return ToNumber(ToPrimitive(o, TypeCode.Double));
  57. }
  58. /// <summary>
  59. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.4
  60. /// </summary>
  61. /// <param name="o"></param>
  62. /// <returns></returns>
  63. public static int ToInteger(object o)
  64. {
  65. return (int) o;
  66. }
  67. /// <summary>
  68. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.5
  69. /// </summary>
  70. /// <param name="o"></param>
  71. /// <returns></returns>
  72. public static int ToInt32(object o)
  73. {
  74. return (int)o;
  75. }
  76. /// <summary>
  77. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.6
  78. /// </summary>
  79. /// <param name="o"></param>
  80. /// <returns></returns>
  81. public static uint ToUint32(object o)
  82. {
  83. return (uint)o;
  84. }
  85. /// <summary>
  86. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.7
  87. /// </summary>
  88. /// <param name="o"></param>
  89. /// <returns></returns>
  90. public static ushort ToUint16(object o)
  91. {
  92. return (ushort)o;
  93. }
  94. /// <summary>
  95. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.8
  96. /// </summary>
  97. /// <param name="o"></param>
  98. /// <returns></returns>
  99. public static string ToString(object o)
  100. {
  101. return (string)o;
  102. }
  103. public static ObjectInstance ToObject(Engine engine, object value)
  104. {
  105. var o = value as ObjectInstance;
  106. if (o != null)
  107. {
  108. return o;
  109. }
  110. if (value == Undefined.Instance)
  111. {
  112. throw new TypeError();
  113. }
  114. if (value == Null.Instance)
  115. {
  116. throw new TypeError();
  117. }
  118. if (value is bool)
  119. {
  120. return engine.Boolean.Construct((bool) value);
  121. }
  122. if (value is int)
  123. {
  124. return engine.Number.Construct((int) value);
  125. }
  126. if (value is uint)
  127. {
  128. return engine.Number.Construct((uint) value);
  129. }
  130. if (value is double)
  131. {
  132. return engine.Number.Construct((double) value);
  133. }
  134. if (value is string)
  135. {
  136. return engine.String.Construct((string) value);
  137. }
  138. throw new TypeError();
  139. }
  140. public static TypeCode GetType(object value)
  141. {
  142. if (value == null || value == Undefined.Instance || value == Null.Instance)
  143. {
  144. return TypeCode.Empty;
  145. }
  146. if (value is string)
  147. {
  148. return TypeCode.String;
  149. }
  150. if (value is double || value is int || value is uint || value is ushort)
  151. {
  152. return TypeCode.Double;
  153. }
  154. if (value is bool)
  155. {
  156. return TypeCode.Boolean;
  157. }
  158. return TypeCode.Object;
  159. }
  160. }
  161. }