Key.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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(TypeConverter.ToString(i));
  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 ? (JsValue) JsString.Create(key.Name) : new JsSymbol(key.Name, key._symbolIdentity);
  53. }
  54. public static implicit operator Key(int value)
  55. {
  56. var keys = indexKeys;
  57. return (uint) value < keys.Length ? keys[value] : BuildKey(value);
  58. }
  59. public static implicit operator Key(uint value)
  60. {
  61. var keys = indexKeys;
  62. return value < keys.Length ? keys[value] : BuildKey(value);
  63. }
  64. public static implicit operator Key(ulong value)
  65. {
  66. var keys = indexKeys;
  67. return value < (ulong) keys.Length ? keys[value] : new Key(value.ToString());
  68. }
  69. [MethodImpl(MethodImplOptions.NoInlining)]
  70. private static Key BuildKey(long value)
  71. {
  72. return new Key(value.ToString());
  73. }
  74. public static implicit operator string(Key key) => key.Name;
  75. public static bool operator ==(in Key a, Key b)
  76. {
  77. return a.HashCode == b.HashCode && a.Name == b.Name && a._symbolIdentity == b._symbolIdentity;
  78. }
  79. public static bool operator !=(in Key a, Key b)
  80. {
  81. return a.HashCode != b.HashCode || a.Name != b.Name || a._symbolIdentity != b._symbolIdentity;
  82. }
  83. public static bool operator ==(in Key a, string b)
  84. {
  85. return a.Name == b && a._symbolIdentity == 0;
  86. }
  87. public static bool operator !=(in Key a, string b)
  88. {
  89. return a.Name != b || a._symbolIdentity > 0;
  90. }
  91. public bool Equals(Key other)
  92. {
  93. return Name == other.Name && _symbolIdentity == other._symbolIdentity;
  94. }
  95. public override bool Equals(object obj)
  96. {
  97. return obj is Key other && Equals(other);
  98. }
  99. public override int GetHashCode() => HashCode;
  100. public override string ToString() => Name;
  101. }
  102. }