TypedArrayValue.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Numerics;
  2. using System.Runtime.InteropServices;
  3. using Jint.Runtime;
  4. namespace Jint.Native.TypedArray;
  5. /// <summary>
  6. /// Container for either double or BigInteger.
  7. /// </summary>
  8. [StructLayout(LayoutKind.Auto)]
  9. internal readonly record struct TypedArrayValue(Types Type, double DoubleValue, BigInteger BigInteger) : IConvertible
  10. {
  11. public static implicit operator TypedArrayValue(double value)
  12. {
  13. return new TypedArrayValue(Types.Number, value, default);
  14. }
  15. public static implicit operator TypedArrayValue(byte value)
  16. {
  17. return new TypedArrayValue(Types.Number, value, default);
  18. }
  19. public static implicit operator TypedArrayValue(int value)
  20. {
  21. return new TypedArrayValue(Types.Number, value, default);
  22. }
  23. public static implicit operator TypedArrayValue(ushort value)
  24. {
  25. return new TypedArrayValue(Types.Number, value, default);
  26. }
  27. public static implicit operator TypedArrayValue(short value)
  28. {
  29. return new TypedArrayValue(Types.Number, value, default);
  30. }
  31. public static implicit operator TypedArrayValue(uint value)
  32. {
  33. return new TypedArrayValue(Types.Number, value, default);
  34. }
  35. public static implicit operator TypedArrayValue(BigInteger value)
  36. {
  37. return new TypedArrayValue(Types.BigInt, default, value);
  38. }
  39. public static implicit operator TypedArrayValue(ulong value)
  40. {
  41. return new TypedArrayValue(Types.BigInt, default, value);
  42. }
  43. public static implicit operator TypedArrayValue(long value)
  44. {
  45. return new TypedArrayValue(Types.BigInt, default, value);
  46. }
  47. public JsValue ToJsValue()
  48. {
  49. return Type == Types.Number
  50. ? JsNumber.Create(DoubleValue)
  51. : JsBigInt.Create(BigInteger);
  52. }
  53. public TypeCode GetTypeCode()
  54. {
  55. ExceptionHelper.ThrowNotImplementedException();
  56. return default;
  57. }
  58. public bool ToBoolean(IFormatProvider? provider)
  59. {
  60. ExceptionHelper.ThrowNotImplementedException();
  61. return default;
  62. }
  63. public char ToChar(IFormatProvider? provider)
  64. {
  65. ExceptionHelper.ThrowNotImplementedException();
  66. return default;
  67. }
  68. public sbyte ToSByte(IFormatProvider? provider)
  69. {
  70. return (sbyte) DoubleValue;
  71. }
  72. public byte ToByte(IFormatProvider? provider)
  73. {
  74. return (byte) DoubleValue;
  75. }
  76. public short ToInt16(IFormatProvider? provider)
  77. {
  78. return (short) DoubleValue;
  79. }
  80. public ushort ToUInt16(IFormatProvider? provider)
  81. {
  82. return (ushort) DoubleValue;
  83. }
  84. public int ToInt32(IFormatProvider? provider)
  85. {
  86. return (int) DoubleValue;
  87. }
  88. public uint ToUInt32(IFormatProvider? provider)
  89. {
  90. return (uint) DoubleValue;
  91. }
  92. public long ToInt64(IFormatProvider? provider)
  93. {
  94. return (long) BigInteger;
  95. }
  96. public ulong ToUInt64(IFormatProvider? provider)
  97. {
  98. return (ulong) BigInteger;
  99. }
  100. public float ToSingle(IFormatProvider? provider)
  101. {
  102. return (float) DoubleValue;
  103. }
  104. public double ToDouble(IFormatProvider? provider)
  105. {
  106. return DoubleValue;
  107. }
  108. public decimal ToDecimal(IFormatProvider? provider)
  109. {
  110. return (decimal) DoubleValue;
  111. }
  112. public DateTime ToDateTime(IFormatProvider? provider)
  113. {
  114. ExceptionHelper.ThrowNotImplementedException();
  115. return default;
  116. }
  117. public string ToString(IFormatProvider? provider)
  118. {
  119. ExceptionHelper.ThrowNotImplementedException();
  120. return default;
  121. }
  122. public object ToType(Type conversionType, IFormatProvider? provider)
  123. {
  124. if (conversionType == typeof(BigInteger) && Type == Types.BigInt)
  125. {
  126. return BigInteger;
  127. }
  128. ExceptionHelper.ThrowNotImplementedException();
  129. return default;
  130. }
  131. }