Key.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Native;
  5. using Jint.Runtime;
  6. namespace Jint
  7. {
  8. /// <summary>
  9. /// Represents a key that Jint uses with pre-calculated hash code
  10. /// as runtime does a lot of repetitive dictionary lookups.
  11. /// </summary>
  12. [DebuggerDisplay("{" + nameof(Name) + "}")]
  13. public readonly struct Key : IEquatable<Key>
  14. {
  15. // lookup for indexer keys
  16. internal static readonly Key[] indexKeys = new Key[TypeConverter.intToString.Length];
  17. static Key()
  18. {
  19. for (uint i = 0; i < indexKeys.Length; ++i)
  20. {
  21. indexKeys[i] = new Key(i.ToString());
  22. }
  23. }
  24. public Key(string name) : this(name, symbolIdentity: 0)
  25. {
  26. }
  27. internal Key(string name, int symbolIdentity)
  28. {
  29. if (name == null)
  30. {
  31. ExceptionHelper.ThrowArgumentException("name cannot be null");
  32. }
  33. Name = name;
  34. HashCode = name.GetHashCode() * 397 ^ symbolIdentity.GetHashCode();
  35. _symbolIdentity = symbolIdentity;
  36. }
  37. public readonly string Name;
  38. internal readonly int HashCode;
  39. internal readonly int _symbolIdentity;
  40. public Types Type => _symbolIdentity == 0 ? Types.String : Types.Symbol;
  41. public bool IsSymbol => _symbolIdentity != 0;
  42. public static implicit operator Key(string name)
  43. {
  44. return new Key(name);
  45. }
  46. public static implicit operator Key(JsSymbol name)
  47. {
  48. return name.ToPropertyKey();
  49. }
  50. public static implicit operator JsValue(in Key key)
  51. {
  52. return !key.IsSymbol
  53. ? (JsValue) JsString.Create(key.Name)
  54. : new JsSymbol(new JsString(key.Name), key._symbolIdentity);
  55. }
  56. public static implicit operator Key(int value)
  57. {
  58. var keys = indexKeys;
  59. return (uint) value < keys.Length ? keys[value] : BuildKey(value);
  60. }
  61. public static implicit operator Key(uint value)
  62. {
  63. var keys = indexKeys;
  64. return value < keys.Length ? keys[value] : BuildKey(value);
  65. }
  66. public static implicit operator Key(ulong value)
  67. {
  68. var keys = indexKeys;
  69. return value < (ulong) keys.Length ? keys[value] : new Key(value.ToString());
  70. }
  71. [MethodImpl(MethodImplOptions.NoInlining)]
  72. private static Key BuildKey(long value)
  73. {
  74. return new Key(value.ToString());
  75. }
  76. public static implicit operator string(Key key) => key.Name;
  77. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  78. public static bool operator ==(in Key a, Key b)
  79. {
  80. return a.HashCode == b.HashCode && a.Name == b.Name && a._symbolIdentity == b._symbolIdentity;
  81. }
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. public static bool operator !=(in Key a, Key b)
  84. {
  85. return a.HashCode != b.HashCode || a.Name != b.Name || a._symbolIdentity != b._symbolIdentity;
  86. }
  87. public static bool operator ==(in Key a, string b)
  88. {
  89. return a.Name == b && a._symbolIdentity == 0;
  90. }
  91. public static bool operator !=(in Key a, string b)
  92. {
  93. return a.Name != b || a._symbolIdentity > 0;
  94. }
  95. public bool Equals(Key other)
  96. {
  97. return Name == other.Name && _symbolIdentity == other._symbolIdentity;
  98. }
  99. public override bool Equals(object obj)
  100. {
  101. return obj is Key other && Equals(other);
  102. }
  103. public override int GetHashCode() => HashCode;
  104. public override string ToString() => Name;
  105. }
  106. }