StringOrSymbol.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/FlyString.h>
  28. #include <LibJS/Runtime/PrimitiveString.h>
  29. #include <LibJS/Runtime/Symbol.h>
  30. #include <LibJS/Runtime/Value.h>
  31. namespace JS {
  32. class StringOrSymbol {
  33. public:
  34. static StringOrSymbol from_value(GlobalObject& global_object, Value value)
  35. {
  36. if (value.is_symbol())
  37. return &value.as_symbol();
  38. if (!value.is_empty())
  39. return value.to_string(global_object);
  40. return {};
  41. }
  42. StringOrSymbol() = default;
  43. StringOrSymbol(const char* chars)
  44. : m_ptr(StringImpl::create(chars).leak_ref())
  45. {
  46. }
  47. StringOrSymbol(const String& string)
  48. : m_ptr(string.impl())
  49. {
  50. ASSERT(!string.is_null());
  51. as_string_impl().ref();
  52. }
  53. StringOrSymbol(const FlyString& string)
  54. : m_ptr(string.impl())
  55. {
  56. ASSERT(!string.is_null());
  57. as_string_impl().ref();
  58. }
  59. ~StringOrSymbol()
  60. {
  61. if (is_string())
  62. as_string_impl().unref();
  63. }
  64. StringOrSymbol(const Symbol* symbol)
  65. : m_ptr(symbol)
  66. {
  67. set_symbol_flag();
  68. }
  69. StringOrSymbol(const StringOrSymbol& other)
  70. {
  71. m_ptr = other.m_ptr;
  72. if (is_string())
  73. as_string_impl().ref();
  74. }
  75. StringOrSymbol(StringOrSymbol&& other)
  76. {
  77. m_ptr = exchange(other.m_ptr, nullptr);
  78. }
  79. ALWAYS_INLINE bool is_valid() const { return m_ptr != nullptr; }
  80. ALWAYS_INLINE bool is_symbol() const { return is_valid() && (bits() & 1ul); }
  81. ALWAYS_INLINE bool is_string() const { return is_valid() && !(bits() & 1ul); }
  82. ALWAYS_INLINE String as_string() const
  83. {
  84. ASSERT(is_string());
  85. return as_string_impl();
  86. }
  87. ALWAYS_INLINE const Symbol* as_symbol() const
  88. {
  89. ASSERT(is_symbol());
  90. return reinterpret_cast<const Symbol*>(bits() & ~1ul);
  91. }
  92. String to_display_string() const
  93. {
  94. if (is_string())
  95. return as_string();
  96. if (is_symbol())
  97. return as_symbol()->to_string();
  98. ASSERT_NOT_REACHED();
  99. }
  100. Value to_value(VM& vm) const
  101. {
  102. if (is_string())
  103. return js_string(vm, as_string());
  104. if (is_symbol())
  105. return const_cast<Symbol*>(as_symbol());
  106. return {};
  107. }
  108. void visit_children(Cell::Visitor& visitor)
  109. {
  110. if (is_symbol())
  111. visitor.visit(const_cast<Symbol*>(as_symbol()));
  112. }
  113. ALWAYS_INLINE bool operator==(const StringOrSymbol& other) const
  114. {
  115. if (is_string())
  116. return other.is_string() && as_string_impl() == other.as_string_impl();
  117. if (is_symbol())
  118. return other.is_symbol() && as_symbol() == other.as_symbol();
  119. return true;
  120. }
  121. StringOrSymbol& operator=(const StringOrSymbol& other)
  122. {
  123. if (this == &other)
  124. return *this;
  125. m_ptr = other.m_ptr;
  126. if (is_string())
  127. as_string_impl().ref();
  128. return *this;
  129. }
  130. StringOrSymbol& operator=(StringOrSymbol&& other)
  131. {
  132. if (this != &other)
  133. m_ptr = exchange(other.m_ptr, nullptr);
  134. return *this;
  135. }
  136. unsigned hash() const
  137. {
  138. if (is_string())
  139. return as_string_impl().hash();
  140. return ptr_hash(as_symbol());
  141. }
  142. private:
  143. ALWAYS_INLINE u64 bits() const
  144. {
  145. return reinterpret_cast<uintptr_t>(m_ptr);
  146. }
  147. ALWAYS_INLINE void set_symbol_flag()
  148. {
  149. m_ptr = reinterpret_cast<const void*>(bits() | 1ul);
  150. }
  151. ALWAYS_INLINE const StringImpl& as_string_impl() const
  152. {
  153. ASSERT(is_string());
  154. return *reinterpret_cast<const StringImpl*>(m_ptr);
  155. }
  156. const void* m_ptr { nullptr };
  157. };
  158. }
  159. template<>
  160. struct AK::Traits<JS::StringOrSymbol> : public GenericTraits<JS::StringOrSymbol> {
  161. static unsigned hash(const JS::StringOrSymbol& key)
  162. {
  163. return key.hash();
  164. }
  165. };