TypedArrayElementType.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. namespace Jint.Native.TypedArray
  2. {
  3. internal enum TypedArrayElementType
  4. {
  5. // we have signed first to make comparison vaster to check if signed or unsigned type
  6. Int8,
  7. Int16,
  8. Int32,
  9. BigInt64,
  10. Float32,
  11. Float64,
  12. Uint8,
  13. Uint8C,
  14. Uint16,
  15. Uint32,
  16. BigUint64
  17. }
  18. internal static class TypedArrayExtensions
  19. {
  20. internal static int GetElementSize(this TypedArrayElementType type)
  21. {
  22. return type switch
  23. {
  24. TypedArrayElementType.Int8 => 1,
  25. TypedArrayElementType.Uint8 => 1,
  26. TypedArrayElementType.Uint8C => 1,
  27. TypedArrayElementType.Int16 => 2,
  28. TypedArrayElementType.Uint16 => 2,
  29. TypedArrayElementType.Int32 => 4,
  30. TypedArrayElementType.Uint32 => 4,
  31. TypedArrayElementType.BigInt64 => 8,
  32. TypedArrayElementType.BigUint64 => 8,
  33. TypedArrayElementType.Float32 => 4,
  34. TypedArrayElementType.Float64 => 8,
  35. _ => -1
  36. };
  37. }
  38. internal static bool IsUnsignedElementType(this TypedArrayElementType type)
  39. {
  40. return type > TypedArrayElementType.Float64;
  41. }
  42. internal static bool IsBigIntElementType(this TypedArrayElementType type)
  43. {
  44. return type is TypedArrayElementType.BigUint64 or TypedArrayElementType.BigInt64;
  45. }
  46. }
  47. }