JsBigInt.cs 2.8 KB

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