StringOrSymbol.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. static_cast<const StringImpl*>(m_ptr)->ref();
  52. }
  53. StringOrSymbol(const FlyString& string)
  54. : m_ptr(string.impl())
  55. {
  56. ASSERT(!string.is_null());
  57. static_cast<const StringImpl*>(m_ptr)->ref();
  58. }
  59. ~StringOrSymbol()
  60. {
  61. if (is_string())
  62. reinterpret_cast<const StringImpl*>(m_ptr)->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. reinterpret_cast<const StringImpl*>(m_ptr)->ref();
  74. }
  75. ALWAYS_INLINE bool is_valid() const { return m_ptr != nullptr; }
  76. ALWAYS_INLINE bool is_symbol() const { return is_valid() && (bits() & 1ul); }
  77. ALWAYS_INLINE bool is_string() const { return is_valid() && !(bits() & 1ul); }
  78. ALWAYS_INLINE String as_string() const
  79. {
  80. ASSERT(is_string());
  81. return reinterpret_cast<const StringImpl*>(m_ptr);
  82. }
  83. ALWAYS_INLINE const Symbol* as_symbol() const
  84. {
  85. ASSERT(is_symbol());
  86. return reinterpret_cast<const Symbol*>(bits() & ~1ul);
  87. }
  88. String to_display_string() const
  89. {
  90. if (is_string())
  91. return as_string();
  92. if (is_symbol())
  93. return as_symbol()->to_string();
  94. ASSERT_NOT_REACHED();
  95. }
  96. Value to_value(VM& vm) const
  97. {
  98. if (is_string())
  99. return js_string(vm, as_string());
  100. if (is_symbol())
  101. return const_cast<Symbol*>(as_symbol());
  102. return {};
  103. }
  104. void visit_children(Cell::Visitor& visitor)
  105. {
  106. if (is_symbol())
  107. visitor.visit(const_cast<Symbol*>(as_symbol()));
  108. }
  109. ALWAYS_INLINE bool operator==(const StringOrSymbol& other) const
  110. {
  111. if (is_string()) {
  112. if (!other.is_string())
  113. return false;
  114. auto* this_impl = static_cast<const StringImpl*>(m_ptr);
  115. auto* other_impl = static_cast<const StringImpl*>(other.m_ptr);
  116. return *this_impl == *other_impl;
  117. }
  118. if (is_symbol())
  119. return other.is_symbol() && as_symbol() == other.as_symbol();
  120. return true;
  121. }
  122. StringOrSymbol& operator=(const StringOrSymbol& other)
  123. {
  124. if (this == &other)
  125. return *this;
  126. m_ptr = other.m_ptr;
  127. if (is_string())
  128. reinterpret_cast<const StringImpl*>(m_ptr)->ref();
  129. return *this;
  130. }
  131. unsigned hash() const
  132. {
  133. if (is_string())
  134. return static_cast<const StringImpl*>(m_ptr)->hash();
  135. return ptr_hash(as_symbol());
  136. }
  137. private:
  138. ALWAYS_INLINE u64 bits() const
  139. {
  140. return reinterpret_cast<uintptr_t>(m_ptr);
  141. }
  142. ALWAYS_INLINE void set_symbol_flag()
  143. {
  144. m_ptr = reinterpret_cast<const void*>(bits() | 1ul);
  145. }
  146. const void* m_ptr { nullptr };
  147. };
  148. }
  149. template<>
  150. struct AK::Traits<JS::StringOrSymbol> : public GenericTraits<JS::StringOrSymbol> {
  151. static unsigned hash(const JS::StringOrSymbol& key)
  152. {
  153. return key.hash();
  154. }
  155. };