JsNumber.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #nullable enable
  2. using System;
  3. using System.Numerics;
  4. using System.Runtime.CompilerServices;
  5. using Jint.Native.Number;
  6. using Jint.Runtime;
  7. namespace Jint.Native
  8. {
  9. public sealed class JsNumber : JsValue, IEquatable<JsNumber>
  10. {
  11. // .NET double epsilon and JS epsilon have different values
  12. internal const double JavaScriptEpsilon = 2.2204460492503130808472633361816E-16;
  13. internal readonly double _value;
  14. // how many decimals to check when determining if double is actually an int
  15. internal const double DoubleIsIntegerTolerance = double.Epsilon * 100;
  16. internal static readonly long NegativeZeroBits = BitConverter.DoubleToInt64Bits(-0.0);
  17. // we can cache most common values, doubles are used in indexing too at times so we also cache
  18. // integer values converted to doubles
  19. private const int NumbersMax = 1024 * 10;
  20. private static readonly JsNumber[] _intToJsValue;
  21. internal static readonly JsNumber DoubleNaN = new JsNumber(double.NaN);
  22. internal static readonly JsNumber DoubleNegativeOne = new JsNumber((double) -1);
  23. internal static readonly JsNumber DoublePositiveInfinity = new JsNumber(double.PositiveInfinity);
  24. internal static readonly JsNumber DoubleNegativeInfinity = new JsNumber(double.NegativeInfinity);
  25. internal static readonly JsNumber IntegerNegativeOne = new JsNumber(-1);
  26. internal static readonly JsNumber NegativeZero = new JsNumber(-0d);
  27. internal static readonly JsNumber PositiveZero = new JsNumber(+0);
  28. internal static readonly JsNumber PositiveOne = new JsNumber(1);
  29. internal static readonly JsNumber PositiveTwo = new JsNumber(2);
  30. internal static readonly JsNumber PositiveThree = new JsNumber(3);
  31. internal static readonly JsNumber PI = new JsNumber(System.Math.PI);
  32. static JsNumber()
  33. {
  34. var integers = new JsNumber[NumbersMax];
  35. for (uint i = 0; i < (uint) integers.Length; i++)
  36. {
  37. integers[i] = new JsNumber(i);
  38. }
  39. _intToJsValue = integers;
  40. }
  41. public JsNumber(double value) : base(Types.Number)
  42. {
  43. _value = value;
  44. }
  45. public JsNumber(int value) : base(InternalTypes.Integer)
  46. {
  47. _value = value;
  48. }
  49. public JsNumber(uint value) : base(value <= int.MaxValue ? InternalTypes.Integer : InternalTypes.Number)
  50. {
  51. _value = value;
  52. }
  53. public JsNumber(ulong value) : base(value <= int.MaxValue ? InternalTypes.Integer : InternalTypes.Number)
  54. {
  55. _value = value;
  56. }
  57. public JsNumber(long value) : base(value <= int.MaxValue && value >= int.MinValue ? InternalTypes.Integer : InternalTypes.Number)
  58. {
  59. _value = value;
  60. }
  61. public override object ToObject()
  62. {
  63. return _value;
  64. }
  65. internal static JsNumber Create(object value)
  66. {
  67. var underlyingType = System.Type.GetTypeCode(Enum.GetUnderlyingType(value.GetType()));
  68. switch (underlyingType)
  69. {
  70. case TypeCode.Int64:
  71. return Create(Convert.ToInt64(value));
  72. case TypeCode.UInt32:
  73. case TypeCode.UInt64:
  74. return Create(Convert.ToUInt64(value));
  75. default:
  76. return Create(Convert.ToInt32(value));
  77. }
  78. }
  79. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  80. internal static JsNumber Create(double value)
  81. {
  82. // we expect zero to be on the fast path of integer mostly
  83. var temp = _intToJsValue;
  84. if (value >= 1 && value < temp.Length && TypeConverter.IsIntegralNumber(value))
  85. {
  86. return temp[(uint) value];
  87. }
  88. if (value == -1)
  89. {
  90. return DoubleNegativeOne;
  91. }
  92. return CreateNumberUnlikely(value);
  93. }
  94. [MethodImpl(MethodImplOptions.NoInlining)]
  95. private static JsNumber CreateNumberUnlikely(double value)
  96. {
  97. if (value <= double.MaxValue && value >= double.MinValue)
  98. {
  99. return new JsNumber(value);
  100. }
  101. if (value == double.NegativeInfinity)
  102. {
  103. return DoubleNegativeInfinity;
  104. }
  105. if (value == double.PositiveInfinity)
  106. {
  107. return DoublePositiveInfinity;
  108. }
  109. if (double.IsNaN(value))
  110. {
  111. return DoubleNaN;
  112. }
  113. return new JsNumber(value);
  114. }
  115. internal static JsNumber Create(byte value)
  116. {
  117. return _intToJsValue[value];
  118. }
  119. internal static JsNumber Create(sbyte value)
  120. {
  121. var temp = _intToJsValue;
  122. if ((uint) value < (uint) temp.Length)
  123. {
  124. return temp[value];
  125. }
  126. return new JsNumber(value);
  127. }
  128. internal static JsNumber Create(int value)
  129. {
  130. var temp = _intToJsValue;
  131. if ((uint) value < (uint) temp.Length)
  132. {
  133. return temp[value];
  134. }
  135. if (value == -1)
  136. {
  137. return IntegerNegativeOne;
  138. }
  139. return new JsNumber(value);
  140. }
  141. internal static JsNumber Create(uint value)
  142. {
  143. var temp = _intToJsValue;
  144. if (value < (uint) temp.Length)
  145. {
  146. return temp[value];
  147. }
  148. return new JsNumber(value);
  149. }
  150. internal static JsNumber Create(ulong value)
  151. {
  152. if (value < (ulong) _intToJsValue.Length)
  153. {
  154. return _intToJsValue[value];
  155. }
  156. return new JsNumber(value);
  157. }
  158. internal static JsNumber Create(long value)
  159. {
  160. if ((ulong) value < (ulong) _intToJsValue.Length)
  161. {
  162. return _intToJsValue[value];
  163. }
  164. return new JsNumber(value);
  165. }
  166. public static JsNumber Create(JsValue jsValue)
  167. {
  168. if (jsValue is JsNumber number)
  169. {
  170. return number;
  171. }
  172. return Create(TypeConverter.ToNumber(jsValue));
  173. }
  174. public override string ToString()
  175. {
  176. return TypeConverter.ToString(_value);
  177. }
  178. internal bool IsNaN()
  179. {
  180. return double.IsNaN(_value);
  181. }
  182. /// <summary>
  183. /// Either positive or negative zero.
  184. /// </summary>
  185. internal bool IsZero()
  186. {
  187. return IsNegativeZero() || IsPositiveZero();
  188. }
  189. internal bool IsNegativeZero()
  190. {
  191. return NumberInstance.IsNegativeZero(_value);
  192. }
  193. internal bool IsPositiveZero()
  194. {
  195. return NumberInstance.IsPositiveZero(_value);
  196. }
  197. internal bool IsPositiveInfinity()
  198. {
  199. return double.IsPositiveInfinity(_value);
  200. }
  201. internal bool IsNegativeInfinity()
  202. {
  203. return double.IsNegativeInfinity(_value);
  204. }
  205. public override bool IsLooselyEqual(JsValue value)
  206. {
  207. if (value is JsNumber jsNumber)
  208. {
  209. return Equals(jsNumber);
  210. }
  211. if (value.IsBigInt())
  212. {
  213. return TypeConverter.IsIntegralNumber(_value) && new BigInteger(_value) == value.AsBigInt();
  214. }
  215. return base.IsLooselyEqual(value);
  216. }
  217. public override bool Equals(JsValue obj)
  218. {
  219. return Equals(obj as JsNumber);
  220. }
  221. public override bool Equals(object? obj)
  222. {
  223. return Equals(obj as JsNumber);
  224. }
  225. public bool Equals(JsNumber? other)
  226. {
  227. if (other is null)
  228. {
  229. return false;
  230. }
  231. if (double.IsNaN(_value) || double.IsNaN(other._value))
  232. {
  233. return false;
  234. }
  235. if (ReferenceEquals(this, other))
  236. {
  237. return true;
  238. }
  239. return _value == other._value;
  240. }
  241. public override int GetHashCode()
  242. {
  243. return _value.GetHashCode();
  244. }
  245. }
  246. }