EvalMapString.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. template <bool fastHash>
  9. struct EvalMapStringInternal
  10. {
  11. FinalizableObject* owningVar; // This is the Var that originally owns the character buffer corresponding to this EvalMap key.
  12. JsUtil::CharacterBuffer<char16> str;
  13. hash_t hash;
  14. ModuleID moduleID;
  15. BOOL strict;
  16. BOOL isLibraryCode;
  17. EvalMapStringInternal() : owningVar(nullptr), str(), moduleID(0), strict(FALSE), isLibraryCode(FALSE), hash(0) {};
  18. EvalMapStringInternal(FinalizableObject* obj, __in_ecount(charLength) char16 const* content, int charLength, ModuleID moduleID, BOOL strict, BOOL isLibraryCode)
  19. : owningVar(obj), str(content, charLength), moduleID(moduleID), strict(strict), isLibraryCode(isLibraryCode)
  20. {
  21. // NOTE: this hash is not equivalent to the character buffer hash
  22. // Don't use a CharacteBuffer to do a map lookup on the EvalMapString.
  23. if (fastHash)
  24. {
  25. hash = TAGHASH(str.FastHash());
  26. }
  27. else
  28. {
  29. hash = TAGHASH((hash_t)str);
  30. }
  31. };
  32. EvalMapStringInternal& operator=(void * str)
  33. {
  34. Assert(str == null);
  35. memset(this, 0, sizeof(*this));
  36. return (*this);
  37. }
  38. inline ModuleID GetModuleID() const
  39. {
  40. return moduleID;
  41. }
  42. inline BOOL IsStrict() const
  43. {
  44. return strict;
  45. }
  46. // Equality and hash function
  47. bool operator==(EvalMapStringInternal const& other) const
  48. {
  49. return this->str == other.str &&
  50. this->GetModuleID() == other.GetModuleID() &&
  51. this->IsStrict() == other.IsStrict() &&
  52. this->isLibraryCode == other.isLibraryCode;
  53. }
  54. operator hash_t() const
  55. {
  56. return UNTAGHASH(hash);
  57. }
  58. };
  59. typedef EvalMapStringInternal<true> FastEvalMapString;
  60. typedef EvalMapStringInternal<false> EvalMapString;
  61. void ConvertKey(const FastEvalMapString& src, EvalMapString& dest);
  62. };