StringOrSymbol.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <LibJS/Runtime/PrimitiveString.h>
  9. #include <LibJS/Runtime/Symbol.h>
  10. #include <LibJS/Runtime/Value.h>
  11. namespace JS {
  12. class StringOrSymbol {
  13. public:
  14. StringOrSymbol() = default;
  15. StringOrSymbol(char const* chars)
  16. : StringOrSymbol(FlyString(chars))
  17. {
  18. }
  19. StringOrSymbol(String const& string)
  20. : StringOrSymbol(FlyString(string))
  21. {
  22. }
  23. StringOrSymbol(FlyString const& string)
  24. : m_ptr(string.impl())
  25. {
  26. VERIFY(!string.is_null());
  27. as_string_impl().ref();
  28. }
  29. ~StringOrSymbol()
  30. {
  31. if (is_string())
  32. as_string_impl().unref();
  33. }
  34. StringOrSymbol(Symbol const* symbol)
  35. : m_ptr(symbol)
  36. {
  37. set_symbol_flag();
  38. }
  39. StringOrSymbol(StringOrSymbol const& other)
  40. {
  41. m_ptr = other.m_ptr;
  42. if (is_string())
  43. as_string_impl().ref();
  44. }
  45. StringOrSymbol(StringOrSymbol&& other)
  46. {
  47. m_ptr = exchange(other.m_ptr, nullptr);
  48. }
  49. ALWAYS_INLINE bool is_valid() const { return m_ptr != nullptr; }
  50. ALWAYS_INLINE bool is_symbol() const { return is_valid() && (bits() & 1ul); }
  51. ALWAYS_INLINE bool is_string() const { return is_valid() && !(bits() & 1ul); }
  52. ALWAYS_INLINE FlyString as_string() const
  53. {
  54. VERIFY(is_string());
  55. return FlyString::from_fly_impl(as_string_impl());
  56. }
  57. ALWAYS_INLINE Symbol const* as_symbol() const
  58. {
  59. VERIFY(is_symbol());
  60. return reinterpret_cast<Symbol const*>(bits() & ~1ul);
  61. }
  62. String to_display_string() const
  63. {
  64. if (is_string())
  65. return as_string();
  66. if (is_symbol())
  67. return as_symbol()->to_string();
  68. VERIFY_NOT_REACHED();
  69. }
  70. Value to_value(VM& vm) const
  71. {
  72. if (is_string())
  73. return js_string(vm, as_string());
  74. if (is_symbol())
  75. return const_cast<Symbol*>(as_symbol());
  76. return {};
  77. }
  78. void visit_edges(Cell::Visitor& visitor)
  79. {
  80. if (is_symbol())
  81. visitor.visit(const_cast<Symbol*>(as_symbol()));
  82. }
  83. ALWAYS_INLINE bool operator==(StringOrSymbol const& other) const
  84. {
  85. if (is_string())
  86. return other.is_string() && &as_string_impl() == &other.as_string_impl();
  87. if (is_symbol())
  88. return other.is_symbol() && as_symbol() == other.as_symbol();
  89. return true;
  90. }
  91. StringOrSymbol& operator=(StringOrSymbol const& other)
  92. {
  93. if (this == &other)
  94. return *this;
  95. m_ptr = other.m_ptr;
  96. if (is_string())
  97. as_string_impl().ref();
  98. return *this;
  99. }
  100. StringOrSymbol& operator=(StringOrSymbol&& other)
  101. {
  102. if (this != &other)
  103. m_ptr = exchange(other.m_ptr, nullptr);
  104. return *this;
  105. }
  106. unsigned hash() const
  107. {
  108. if (is_string())
  109. return as_string_impl().hash();
  110. return ptr_hash(as_symbol());
  111. }
  112. private:
  113. ALWAYS_INLINE u64 bits() const
  114. {
  115. return reinterpret_cast<uintptr_t>(m_ptr);
  116. }
  117. ALWAYS_INLINE void set_symbol_flag()
  118. {
  119. m_ptr = reinterpret_cast<void const*>(bits() | 1ul);
  120. }
  121. ALWAYS_INLINE StringImpl const& as_string_impl() const
  122. {
  123. VERIFY(is_string());
  124. return *reinterpret_cast<StringImpl const*>(m_ptr);
  125. }
  126. void const* m_ptr { nullptr };
  127. };
  128. }
  129. template<>
  130. struct AK::Traits<JS::StringOrSymbol> : public GenericTraits<JS::StringOrSymbol> {
  131. static unsigned hash(JS::StringOrSymbol const& key)
  132. {
  133. return key.hash();
  134. }
  135. };