2
0

JsBigInt.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Numerics;
  3. using Jint.Runtime;
  4. namespace Jint.Native;
  5. public sealed class JsBigInt : JsValue, IEquatable<JsBigInt>
  6. {
  7. internal readonly BigInteger _value;
  8. public static readonly JsBigInt Zero = new(0);
  9. public static readonly JsBigInt One = new(1);
  10. private static readonly JsBigInt[] _bigIntegerToJsValue;
  11. static JsBigInt()
  12. {
  13. var bigIntegers = new JsBigInt[1024];
  14. for (uint i = 0; i < bigIntegers.Length; i++)
  15. {
  16. bigIntegers[i] = new JsBigInt(i);
  17. }
  18. _bigIntegerToJsValue = bigIntegers;
  19. }
  20. public JsBigInt(BigInteger value) : base(Types.BigInt)
  21. {
  22. _value = value;
  23. }
  24. internal static JsBigInt Create(BigInteger bigInt)
  25. {
  26. var temp = _bigIntegerToJsValue;
  27. if (bigInt >= 0 && bigInt < (uint) temp.Length)
  28. {
  29. return temp[(int) bigInt];
  30. }
  31. return new JsBigInt(bigInt);
  32. }
  33. internal static JsBigInt Create(JsValue value)
  34. {
  35. return value is JsBigInt jsBigInt
  36. ? jsBigInt
  37. : Create(TypeConverter.ToBigInt(value));
  38. }
  39. public override object ToObject()
  40. {
  41. return _value;
  42. }
  43. public static bool operator ==(JsBigInt a, double b)
  44. {
  45. return a is not null && TypeConverter.IsIntegralNumber(b) && a._value == (long) b;
  46. }
  47. public static bool operator !=(JsBigInt a, double b)
  48. {
  49. return !(a == b);
  50. }
  51. public override string ToString()
  52. {
  53. return TypeConverter.ToString(_value);
  54. }
  55. public override bool IsLooselyEqual(JsValue value)
  56. {
  57. if (value is JsBigInt bigInt)
  58. {
  59. return Equals(bigInt);
  60. }
  61. if (value is JsNumber number && TypeConverter.IsIntegralNumber(number._value) && _value == new BigInteger(number._value))
  62. {
  63. return true;
  64. }
  65. if (value is JsBoolean b)
  66. {
  67. return b._value && _value == BigInteger.One || !b._value && _value == BigInteger.Zero;
  68. }
  69. if (value is JsString s && TypeConverter.TryStringToBigInt(s.ToString(), out var temp) && temp == _value)
  70. {
  71. return true;
  72. }
  73. if (value.IsObject())
  74. {
  75. return IsLooselyEqual(TypeConverter.ToPrimitive(value, Types.Number));
  76. }
  77. return false;
  78. }
  79. public override bool Equals(object other)
  80. {
  81. return Equals(other as JsBigInt);
  82. }
  83. public override bool Equals(JsValue other)
  84. {
  85. return Equals(other as JsBigInt);
  86. }
  87. public bool Equals(JsBigInt other)
  88. {
  89. if (ReferenceEquals(null, other))
  90. {
  91. return false;
  92. }
  93. return ReferenceEquals(this, other) || _value.Equals(other._value);
  94. }
  95. public override int GetHashCode()
  96. {
  97. return _value.GetHashCode();
  98. }
  99. }