TypeConverter.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 JsValue ToPrimitive(JsValue input, Types preferredType = Types.None)
  27. {
  28. if (input == Null.Instance || input == Undefined.Instance)
  29. {
  30. return input;
  31. }
  32. if (input.IsPrimitive())
  33. {
  34. return input;
  35. }
  36. return input.AsObject().DefaultValue(preferredType);
  37. }
  38. /// <summary>
  39. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
  40. /// </summary>
  41. /// <param name="o"></param>
  42. /// <returns></returns>
  43. public static bool ToBoolean(JsValue o)
  44. {
  45. if (o.IsObject())
  46. {
  47. return true;
  48. }
  49. if (o == Undefined.Instance || o == Null.Instance)
  50. {
  51. return false;
  52. }
  53. if (o.IsBoolean())
  54. {
  55. return o.AsBoolean();
  56. }
  57. if (o.IsNumber())
  58. {
  59. var n = o.AsNumber();
  60. if (n.Equals(0) || double.IsNaN(n))
  61. {
  62. return false;
  63. }
  64. else
  65. {
  66. return true;
  67. }
  68. }
  69. if (o.IsString())
  70. {
  71. var s = o.AsString();
  72. if (String.IsNullOrEmpty(s))
  73. {
  74. return false;
  75. }
  76. else
  77. {
  78. return true;
  79. }
  80. }
  81. return true;
  82. }
  83. /// <summary>
  84. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.3
  85. /// </summary>
  86. /// <param name="o"></param>
  87. /// <returns></returns>
  88. public static double ToNumber(JsValue o)
  89. {
  90. if (o.IsObject())
  91. {
  92. var p = o.AsObject() as IPrimitiveInstance;
  93. if (p != null)
  94. {
  95. o = p.PrimitiveValue;
  96. }
  97. }
  98. if (o.IsNumber())
  99. {
  100. return o.AsNumber();
  101. }
  102. if (o == Undefined.Instance)
  103. {
  104. return double.NaN;
  105. }
  106. if (o == Null.Instance)
  107. {
  108. return 0;
  109. }
  110. if (o.IsBoolean())
  111. {
  112. return o.AsBoolean() ? 1 : 0;
  113. }
  114. if (o.IsString())
  115. {
  116. var s = o.AsString().Trim();
  117. if (String.IsNullOrEmpty(s))
  118. {
  119. return 0;
  120. }
  121. if ("+Infinity".Equals(s) || "Infinity".Equals(s))
  122. {
  123. return double.PositiveInfinity;
  124. }
  125. if ("-Infinity".Equals(s))
  126. {
  127. return double.NegativeInfinity;
  128. }
  129. // todo: use a common implementation with JavascriptParser
  130. try
  131. {
  132. if (!s.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
  133. {
  134. var start = s[0];
  135. if (start != '+' && start != '-' && start != '.' && !char.IsDigit(start))
  136. {
  137. return double.NaN;
  138. }
  139. double n = Double.Parse(s,
  140. NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign |
  141. NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite |
  142. NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
  143. if (s.StartsWith("-") && n.Equals(0))
  144. {
  145. return -0.0;
  146. }
  147. return n;
  148. }
  149. int i = int.Parse(s.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
  150. return i;
  151. }
  152. catch (OverflowException)
  153. {
  154. return s.StartsWith("-") ? double.NegativeInfinity : double.PositiveInfinity;
  155. }
  156. catch
  157. {
  158. return double.NaN;
  159. }
  160. }
  161. return ToNumber(ToPrimitive(o, Types.Number));
  162. }
  163. /// <summary>
  164. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.4
  165. /// </summary>
  166. /// <param name="o"></param>
  167. /// <returns></returns>
  168. public static double ToInteger(JsValue o)
  169. {
  170. var number = ToNumber(o);
  171. if (double.IsNaN(number))
  172. {
  173. return 0;
  174. }
  175. if (number.Equals(0) || double.IsInfinity(number))
  176. {
  177. return number;
  178. }
  179. return (long)number;
  180. }
  181. /// <summary>
  182. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.5
  183. /// </summary>
  184. /// <param name="o"></param>
  185. /// <returns></returns>
  186. public static int ToInt32(JsValue o)
  187. {
  188. return (int)(uint)ToNumber(o);
  189. }
  190. /// <summary>
  191. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.6
  192. /// </summary>
  193. /// <param name="o"></param>
  194. /// <returns></returns>
  195. public static uint ToUint32(JsValue o)
  196. {
  197. return (uint)ToNumber(o);
  198. }
  199. /// <summary>
  200. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.7
  201. /// </summary>
  202. /// <param name="o"></param>
  203. /// <returns></returns>
  204. public static ushort ToUint16(JsValue o)
  205. {
  206. return (ushort)(uint)ToNumber(o);
  207. }
  208. /// <summary>
  209. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.8
  210. /// </summary>
  211. /// <param name="o"></param>
  212. /// <returns></returns>
  213. public static string ToString(JsValue o)
  214. {
  215. if (o.IsObject())
  216. {
  217. var p = o.AsObject() as IPrimitiveInstance;
  218. if (p != null)
  219. {
  220. o = p.PrimitiveValue;
  221. }
  222. }
  223. if (o.IsString())
  224. {
  225. return o.AsString();
  226. }
  227. if (o == Undefined.Instance)
  228. {
  229. return Undefined.Text;
  230. }
  231. if (o == Null.Instance)
  232. {
  233. return Null.Text;
  234. }
  235. if (o.IsBoolean())
  236. {
  237. return o.AsBoolean() ? "true" : "false";
  238. }
  239. if (o.IsNumber())
  240. {
  241. return NumberPrototype.ToNumberString(o.AsNumber());
  242. }
  243. return ToString(ToPrimitive(o, Types.String));
  244. }
  245. public static ObjectInstance ToObject(Engine engine, JsValue value)
  246. {
  247. if (value.IsObject())
  248. {
  249. return value.AsObject();
  250. }
  251. if (value == Undefined.Instance)
  252. {
  253. throw new JavaScriptException(engine.TypeError);
  254. }
  255. if (value == Null.Instance)
  256. {
  257. throw new JavaScriptException(engine.TypeError);
  258. }
  259. if (value.IsBoolean())
  260. {
  261. return engine.Boolean.Construct(value.AsBoolean());
  262. }
  263. if (value.IsNumber())
  264. {
  265. return engine.Number.Construct(value.AsNumber());
  266. }
  267. if (value.IsString())
  268. {
  269. return engine.String.Construct(value.AsString());
  270. }
  271. throw new JavaScriptException(engine.TypeError);
  272. }
  273. public static Types GetPrimitiveType(JsValue value)
  274. {
  275. if (value.IsObject())
  276. {
  277. var primitive = value.TryCast<IPrimitiveInstance>();
  278. if (primitive != null)
  279. {
  280. return primitive.Type;
  281. }
  282. return Types.Object;
  283. }
  284. return value.Type;
  285. }
  286. public static void CheckObjectCoercible(Engine engine, JsValue o)
  287. {
  288. if (o == Undefined.Instance || o == Null.Instance)
  289. {
  290. throw new JavaScriptException(engine.TypeError);
  291. }
  292. }
  293. }
  294. }