TypeConverter.cs 12 KB

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