TypeConverter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using System;
  2. using System.Globalization;
  3. using Jint.Native;
  4. using Jint.Native.Number;
  5. using Jint.Native.Object;
  6. namespace Jint.Runtime
  7. {
  8. public enum Types
  9. {
  10. None,
  11. Undefined,
  12. Null,
  13. Boolean,
  14. String,
  15. Number,
  16. Object
  17. }
  18. public class TypeConverter
  19. {
  20. /// <summary>
  21. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.1
  22. /// </summary>
  23. /// <param name="input"></param>
  24. /// <param name="preferredType"></param>
  25. /// <returns></returns>
  26. public static object ToPrimitive(object input, Types preferredType = Types.None)
  27. {
  28. if (input == Null.Instance || input == Undefined.Instance)
  29. {
  30. return input;
  31. }
  32. var primitive = input as IPrimitiveType;
  33. if (primitive != null)
  34. {
  35. return primitive.PrimitiveValue;
  36. }
  37. var o = input as ObjectInstance;
  38. if (o == null)
  39. {
  40. return input;
  41. }
  42. return o.DefaultValue(preferredType);
  43. }
  44. public static bool IsPrimitiveValue(object o)
  45. {
  46. return o is string
  47. || o is double
  48. || o is bool
  49. || o is Undefined
  50. || o is Null;
  51. }
  52. /// <summary>
  53. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
  54. /// </summary>
  55. /// <param name="o"></param>
  56. /// <returns></returns>
  57. public static bool ToBoolean(object o)
  58. {
  59. if (o is bool)
  60. {
  61. return (bool)o;
  62. }
  63. if (o == Undefined.Instance || o == Null.Instance)
  64. {
  65. return false;
  66. }
  67. var p = o as IPrimitiveType;
  68. if (p != null)
  69. {
  70. o = ToBoolean(p.PrimitiveValue);
  71. }
  72. if (o is double)
  73. {
  74. var n = (double) o;
  75. if (n == 0 || double.IsNaN(n))
  76. {
  77. return false;
  78. }
  79. else
  80. {
  81. return true;
  82. }
  83. }
  84. var s = o as string;
  85. if (s != null)
  86. {
  87. if (String.IsNullOrEmpty(s))
  88. {
  89. return false;
  90. }
  91. else
  92. {
  93. return true;
  94. }
  95. }
  96. if (o is int)
  97. {
  98. return ToBoolean((double)(int)o);
  99. }
  100. if (o is uint)
  101. {
  102. return ToBoolean((double)(uint)o);
  103. }
  104. return true;
  105. }
  106. /// <summary>
  107. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.3
  108. /// </summary>
  109. /// <param name="o"></param>
  110. /// <returns></returns>
  111. public static double ToNumber(object o)
  112. {
  113. if (o is double)
  114. {
  115. return (double)o;
  116. }
  117. if (o is int)
  118. {
  119. return (int) o;
  120. }
  121. if (o is uint)
  122. {
  123. return (uint)o;
  124. }
  125. if (o is DateTime)
  126. {
  127. return ((DateTime)o).Ticks;
  128. }
  129. if (o == Undefined.Instance)
  130. {
  131. return double.NaN;
  132. }
  133. if (o == Null.Instance)
  134. {
  135. return 0;
  136. }
  137. if (o is bool)
  138. {
  139. return (bool)o ? 1 : 0;
  140. }
  141. var s = o as string;
  142. if (s != null)
  143. {
  144. double n;
  145. s = s.Trim();
  146. if (String.IsNullOrEmpty(s))
  147. {
  148. return 0;
  149. }
  150. if ("+Infinity".Equals(s) || "Infinity".Equals(s))
  151. {
  152. return double.PositiveInfinity;
  153. }
  154. if ("-Infinity".Equals(s))
  155. {
  156. return double.NegativeInfinity;
  157. }
  158. // todo: use a common implementation with JavascriptParser
  159. try
  160. {
  161. if (!s.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
  162. {
  163. var start = s[0];
  164. if (start != '+' && start != '-' && start != '.' && !char.IsDigit(start))
  165. {
  166. return double.NaN;
  167. }
  168. n = Double.Parse(s,
  169. NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign |
  170. NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite |
  171. NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
  172. if (s.StartsWith("-") && n == 0)
  173. {
  174. return -0.0;
  175. }
  176. return n;
  177. }
  178. int i = int.Parse(s.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
  179. return i;
  180. }
  181. catch (OverflowException)
  182. {
  183. return s.StartsWith("-") ? double.NegativeInfinity : double.PositiveInfinity;
  184. }
  185. catch
  186. {
  187. return double.NaN;
  188. }
  189. return double.NaN;
  190. }
  191. return ToNumber(ToPrimitive(o, Types.Number));
  192. }
  193. /// <summary>
  194. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.4
  195. /// </summary>
  196. /// <param name="o"></param>
  197. /// <returns></returns>
  198. public static double ToInteger(object o)
  199. {
  200. var number = ToNumber(o);
  201. if (double.IsNaN(number))
  202. {
  203. return 0;
  204. }
  205. if (number == 0 || number == double.NegativeInfinity || number == double.PositiveInfinity)
  206. {
  207. return number;
  208. }
  209. return (long)number;
  210. }
  211. /// <summary>
  212. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.5
  213. /// </summary>
  214. /// <param name="o"></param>
  215. /// <returns></returns>
  216. public static int ToInt32(object o)
  217. {
  218. if (o is int)
  219. {
  220. return (int)o;
  221. }
  222. return (int)(uint)ToNumber(o);
  223. }
  224. /// <summary>
  225. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.6
  226. /// </summary>
  227. /// <param name="o"></param>
  228. /// <returns></returns>
  229. public static uint ToUint32(object o)
  230. {
  231. if (o is uint)
  232. {
  233. return (uint)o;
  234. }
  235. return (uint)ToNumber(o);
  236. }
  237. /// <summary>
  238. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.7
  239. /// </summary>
  240. /// <param name="o"></param>
  241. /// <returns></returns>
  242. public static ushort ToUint16(object o)
  243. {
  244. return (ushort)(uint)ToNumber(o);
  245. }
  246. /// <summary>
  247. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.8
  248. /// </summary>
  249. /// <param name="o"></param>
  250. /// <returns></returns>
  251. public static string ToString(object o)
  252. {
  253. var str = o as string;
  254. if (str != null)
  255. {
  256. return str;
  257. }
  258. if (o == Undefined.Instance)
  259. {
  260. return Undefined.Instance.ToString();
  261. }
  262. if (o == Null.Instance)
  263. {
  264. return Null.Instance.ToString();
  265. }
  266. var p = o as IPrimitiveType;
  267. if (p != null)
  268. {
  269. o = p.PrimitiveValue;
  270. }
  271. if (o is bool)
  272. {
  273. return (bool) o ? "true" : "false";
  274. }
  275. if (o is double)
  276. {
  277. var m = (double) o;
  278. return NumberPrototype.ToNumberString(m);
  279. }
  280. if (o is int)
  281. {
  282. return ToString((double)(int)o);
  283. }
  284. if (o is uint)
  285. {
  286. return ToString((double)(uint)o);
  287. }
  288. if (o is DateTime)
  289. {
  290. return o.ToString();
  291. }
  292. return ToString(ToPrimitive(o, Types.String));
  293. }
  294. public static ObjectInstance ToObject(Engine engine, object value)
  295. {
  296. var o = value as ObjectInstance;
  297. if (o != null)
  298. {
  299. return o;
  300. }
  301. if (value == Undefined.Instance)
  302. {
  303. throw new JavaScriptException(engine.TypeError);
  304. }
  305. if (value == Null.Instance)
  306. {
  307. throw new JavaScriptException(engine.TypeError);
  308. }
  309. if (value is bool)
  310. {
  311. return engine.Boolean.Construct((bool) value);
  312. }
  313. if (value is int)
  314. {
  315. return engine.Number.Construct((int) value);
  316. }
  317. if (value is uint)
  318. {
  319. return engine.Number.Construct((uint) value);
  320. }
  321. if (value is DateTime)
  322. {
  323. return engine.Date.Construct((DateTime)value);
  324. }
  325. if (value is double)
  326. {
  327. return engine.Number.Construct((double) value);
  328. }
  329. var s = value as string;
  330. if (s != null)
  331. {
  332. return engine.String.Construct(s);
  333. }
  334. throw new JavaScriptException(engine.TypeError);
  335. }
  336. public static Types GetType(object value)
  337. {
  338. if (value == null || value == Null.Instance)
  339. {
  340. return Types.Null;
  341. }
  342. if (value == Undefined.Instance)
  343. {
  344. return Types.Undefined;
  345. }
  346. if (value is string)
  347. {
  348. return Types.String;
  349. }
  350. if (value is double || value is int || value is uint || value is ushort)
  351. {
  352. return Types.Number;
  353. }
  354. if (value is bool)
  355. {
  356. return Types.Boolean;
  357. }
  358. return Types.Object;
  359. }
  360. public static void CheckObjectCoercible(Engine engine, object o)
  361. {
  362. if (o == Undefined.Instance || o == Null.Instance)
  363. {
  364. throw new JavaScriptException(engine.TypeError);
  365. }
  366. }
  367. }
  368. }