TypeConverter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. using Esprima.Ast;
  8. using Jint.Native;
  9. using Jint.Native.Number;
  10. using Jint.Native.Object;
  11. using Jint.Native.String;
  12. using Jint.Runtime.References;
  13. using Jint.Native.Symbol;
  14. namespace Jint.Runtime
  15. {
  16. public enum Types
  17. {
  18. None,
  19. Undefined,
  20. Null,
  21. Boolean,
  22. String,
  23. Number,
  24. Object,
  25. Completion,
  26. Symbol
  27. }
  28. public class TypeConverter
  29. {
  30. // how many decimals to check when determining if double is actually an int
  31. private const double DoubleIsIntegerTolerance = double.Epsilon * 100;
  32. private static readonly string[] intToString = new string[1024];
  33. private static readonly string[] charToString = new string[256];
  34. static TypeConverter()
  35. {
  36. for (var i = 0; i < intToString.Length; ++i)
  37. {
  38. intToString[i] = i.ToString();
  39. }
  40. for (var i = 0; i < charToString.Length; ++i)
  41. {
  42. var c = (char) i;
  43. charToString[i] = c.ToString();
  44. }
  45. }
  46. /// <summary>
  47. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.1
  48. /// </summary>
  49. /// <param name="input"></param>
  50. /// <param name="preferredType"></param>
  51. /// <returns></returns>
  52. public static JsValue ToPrimitive(JsValue input, Types preferredType = Types.None)
  53. {
  54. if (input == Null.Instance || input == Undefined.Instance)
  55. {
  56. return input;
  57. }
  58. if (input.IsPrimitive())
  59. {
  60. return input;
  61. }
  62. return input.AsObject().DefaultValue(preferredType);
  63. }
  64. /// <summary>
  65. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
  66. /// </summary>
  67. /// <param name="o"></param>
  68. /// <returns></returns>
  69. public static bool ToBoolean(JsValue o)
  70. {
  71. if (o.IsObject())
  72. {
  73. return true;
  74. }
  75. if (o.IsBoolean())
  76. {
  77. return o.AsBoolean();
  78. }
  79. if (o == Undefined.Instance || o == Null.Instance)
  80. {
  81. return false;
  82. }
  83. if (o.IsNumber())
  84. {
  85. var n = o.AsNumber();
  86. if (n.Equals(0) || double.IsNaN(n))
  87. {
  88. return false;
  89. }
  90. return true;
  91. }
  92. if (o.IsString())
  93. {
  94. var s = o.AsString();
  95. if (String.IsNullOrEmpty(s))
  96. {
  97. return false;
  98. }
  99. return true;
  100. }
  101. return true;
  102. }
  103. /// <summary>
  104. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.3
  105. /// </summary>
  106. /// <param name="o"></param>
  107. /// <returns></returns>
  108. public static double ToNumber(JsValue o)
  109. {
  110. // check number first as this is what is usually expected
  111. if (o.IsNumber())
  112. {
  113. return o.AsNumber();
  114. }
  115. if (o.IsObject())
  116. {
  117. var p = o.AsObject() as IPrimitiveInstance;
  118. if (p != null)
  119. {
  120. o = p.PrimitiveValue;
  121. }
  122. }
  123. if (o == Undefined.Instance)
  124. {
  125. return double.NaN;
  126. }
  127. if (o == Null.Instance)
  128. {
  129. return 0;
  130. }
  131. if (o.IsBoolean())
  132. {
  133. return o.AsBoolean() ? 1 : 0;
  134. }
  135. if (o.IsString())
  136. {
  137. return ToNumber(o.AsString());
  138. }
  139. return ToNumber(ToPrimitive(o, Types.Number));
  140. }
  141. private static double ToNumber(string input)
  142. {
  143. // eager checks to save time and trimming
  144. if (string.IsNullOrEmpty(input))
  145. {
  146. return 0;
  147. }
  148. var s = StringPrototype.TrimEx(input);
  149. if (string.IsNullOrEmpty(s))
  150. {
  151. return 0;
  152. }
  153. if ("+Infinity".Equals(s) || "Infinity".Equals(s))
  154. {
  155. return double.PositiveInfinity;
  156. }
  157. if ("-Infinity".Equals(s))
  158. {
  159. return double.NegativeInfinity;
  160. }
  161. // todo: use a common implementation with JavascriptParser
  162. try
  163. {
  164. if (!s.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
  165. {
  166. var start = s[0];
  167. if (start != '+' && start != '-' && start != '.' && !char.IsDigit(start))
  168. {
  169. return double.NaN;
  170. }
  171. double n = Double.Parse(s,
  172. NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign |
  173. NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite |
  174. NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
  175. if (s.StartsWith("-") && n.Equals(0))
  176. {
  177. return -0.0;
  178. }
  179. return n;
  180. }
  181. int i = int.Parse(s.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
  182. return i;
  183. }
  184. catch (OverflowException)
  185. {
  186. return s.StartsWith("-") ? double.NegativeInfinity : double.PositiveInfinity;
  187. }
  188. catch
  189. {
  190. return double.NaN;
  191. }
  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(JsValue o)
  199. {
  200. var number = ToNumber(o);
  201. if (double.IsNaN(number))
  202. {
  203. return 0;
  204. }
  205. if (number.Equals(0) || double.IsInfinity(number))
  206. {
  207. return number;
  208. }
  209. return (long) number;
  210. }
  211. internal static double ToInteger(string o)
  212. {
  213. var number = ToNumber(o);
  214. if (double.IsNaN(number))
  215. {
  216. return 0;
  217. }
  218. if (number.Equals(0) || double.IsInfinity(number))
  219. {
  220. return number;
  221. }
  222. return (long) number;
  223. }
  224. /// <summary>
  225. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.5
  226. /// </summary>
  227. /// <param name="o"></param>
  228. /// <returns></returns>
  229. public static int ToInt32(JsValue o)
  230. {
  231. return (int) (uint) ToNumber(o);
  232. }
  233. /// <summary>
  234. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.6
  235. /// </summary>
  236. /// <param name="o"></param>
  237. /// <returns></returns>
  238. public static uint ToUint32(JsValue o)
  239. {
  240. return (uint) ToNumber(o);
  241. }
  242. /// <summary>
  243. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.7
  244. /// </summary>
  245. /// <param name="o"></param>
  246. /// <returns></returns>
  247. public static ushort ToUint16(JsValue o)
  248. {
  249. return (ushort) (uint) ToNumber(o);
  250. }
  251. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  252. internal static string ToString(long i)
  253. {
  254. return i >= 0 && i < intToString.Length
  255. ? intToString[i]
  256. : i.ToString();
  257. }
  258. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  259. internal static string ToString(int i)
  260. {
  261. return i >= 0 && i < intToString.Length
  262. ? intToString[i]
  263. : i.ToString();
  264. }
  265. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  266. internal static string ToString(uint i)
  267. {
  268. return i >= 0 && i < intToString.Length
  269. ? intToString[i]
  270. : i.ToString();
  271. }
  272. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  273. internal static string ToString(char c)
  274. {
  275. return c >= 0 && c < charToString.Length
  276. ? charToString[c]
  277. : c.ToString();
  278. }
  279. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  280. internal static string ToString(double d)
  281. {
  282. if (d > long.MinValue && d < long.MaxValue && Math.Abs(d % 1) <= DoubleIsIntegerTolerance)
  283. {
  284. // we are dealing with integer that can be cached
  285. return ToString((long) d);
  286. }
  287. return NumberPrototype.ToNumberString(d);
  288. }
  289. /// <summary>
  290. /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.8
  291. /// </summary>
  292. /// <param name="o"></param>
  293. /// <returns></returns>
  294. public static string ToString(JsValue o)
  295. {
  296. if (o.IsString())
  297. {
  298. return o.AsString();
  299. }
  300. if (o.IsObject())
  301. {
  302. var p = o.AsObject() as IPrimitiveInstance;
  303. if (p != null)
  304. {
  305. o = p.PrimitiveValue;
  306. }
  307. else
  308. {
  309. var s = o.AsInstance<SymbolInstance>();
  310. if (s != null)
  311. {
  312. // TODO: throw a TypeError
  313. // NB: But it requires an Engine reference
  314. throw new JavaScriptException(new JsValue("TypeError"));
  315. }
  316. }
  317. }
  318. if (o == Undefined.Instance)
  319. {
  320. return Undefined.Text;
  321. }
  322. if (o == Null.Instance)
  323. {
  324. return Null.Text;
  325. }
  326. if (o.IsBoolean())
  327. {
  328. return o.AsBoolean() ? "true" : "false";
  329. }
  330. if (o.IsNumber())
  331. {
  332. return ToString(o.AsNumber());
  333. }
  334. if (o.IsSymbol())
  335. {
  336. return o.AsSymbol();
  337. }
  338. return ToString(ToPrimitive(o, Types.String));
  339. }
  340. public static ObjectInstance ToObject(Engine engine, JsValue value)
  341. {
  342. if (value.IsObject())
  343. {
  344. return value.AsObject();
  345. }
  346. if (value == Undefined.Instance)
  347. {
  348. throw new JavaScriptException(engine.TypeError);
  349. }
  350. if (value == Null.Instance)
  351. {
  352. throw new JavaScriptException(engine.TypeError);
  353. }
  354. if (value.IsBoolean())
  355. {
  356. return engine.Boolean.Construct(value.AsBoolean());
  357. }
  358. if (value.IsNumber())
  359. {
  360. return engine.Number.Construct(value.AsNumber());
  361. }
  362. if (value.IsString())
  363. {
  364. return engine.String.Construct(value.AsString());
  365. }
  366. if (value.IsSymbol())
  367. {
  368. return engine.Symbol.Construct(value.AsSymbol());
  369. }
  370. throw new JavaScriptException(engine.TypeError);
  371. }
  372. public static Types GetPrimitiveType(JsValue value)
  373. {
  374. if (value.IsObject())
  375. {
  376. var primitive = value.TryCast<IPrimitiveInstance>();
  377. if (primitive != null)
  378. {
  379. return primitive.Type;
  380. }
  381. return Types.Object;
  382. }
  383. return value.Type;
  384. }
  385. public static void CheckObjectCoercible(Engine engine, JsValue o, MemberExpression expression,
  386. object baseReference)
  387. {
  388. if (o != Undefined.Instance && o != Null.Instance)
  389. return;
  390. if (engine.Options._ReferenceResolver != null &&
  391. engine.Options._ReferenceResolver.CheckCoercible(o))
  392. return;
  393. var message = string.Empty;
  394. var reference = baseReference as Reference;
  395. if (reference != null)
  396. message = $"{reference.GetReferencedName()} is {o}";
  397. throw new JavaScriptException(engine.TypeError, message)
  398. .SetCallstack(engine, expression.Location);
  399. }
  400. public static void CheckObjectCoercible(Engine engine, JsValue o)
  401. {
  402. if (o == Undefined.Instance || o == Null.Instance)
  403. {
  404. throw new JavaScriptException(engine.TypeError);
  405. }
  406. }
  407. public static IEnumerable<MethodBase> FindBestMatch(Engine engine, MethodBase[] methods, JsValue[] arguments)
  408. {
  409. methods = methods
  410. .Where(m => m.GetParameters().Length == arguments.Length)
  411. .ToArray();
  412. if (methods.Length == 1 && !methods[0].GetParameters().Any())
  413. {
  414. yield return methods[0];
  415. yield break;
  416. }
  417. var objectArguments = arguments.Select(x => x.ToObject()).ToArray();
  418. foreach (var method in methods)
  419. {
  420. var perfectMatch = true;
  421. var parameters = method.GetParameters();
  422. for (var i = 0; i < arguments.Length; i++)
  423. {
  424. var arg = objectArguments[i];
  425. var paramType = parameters[i].ParameterType;
  426. if (arg == null)
  427. {
  428. if (!TypeIsNullable(paramType))
  429. {
  430. perfectMatch = false;
  431. break;
  432. }
  433. }
  434. else if (arg.GetType() != paramType)
  435. {
  436. perfectMatch = false;
  437. break;
  438. }
  439. }
  440. if (perfectMatch)
  441. {
  442. yield return method;
  443. yield break;
  444. }
  445. }
  446. foreach (var method in methods)
  447. {
  448. yield return method;
  449. }
  450. }
  451. public static bool TypeIsNullable(Type type)
  452. {
  453. return !type.IsValueType() || Nullable.GetUnderlyingType(type) != null;
  454. }
  455. }
  456. }