StringOrSymbol.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <LibJS/Runtime/PrimitiveString.h>
  28. #include <LibJS/Runtime/Symbol.h>
  29. #include <LibJS/Runtime/Value.h>
  30. namespace JS {
  31. class StringOrSymbol {
  32. public:
  33. static StringOrSymbol from_value(Interpreter& interpreter, Value value)
  34. {
  35. if (value.is_symbol())
  36. return &value.as_symbol();
  37. if (!value.is_empty())
  38. return value.to_string(interpreter);
  39. return {};
  40. }
  41. StringOrSymbol() = default;
  42. StringOrSymbol(const char* chars)
  43. : m_ptr(StringImpl::create(chars).leak_ref())
  44. {
  45. }
  46. StringOrSymbol(const String& string)
  47. : m_ptr(StringImpl::create(string.characters(), string.length()).leak_ref())
  48. {
  49. }
  50. ~StringOrSymbol()
  51. {
  52. if (is_string())
  53. reinterpret_cast<const StringImpl*>(m_ptr)->unref();
  54. }
  55. StringOrSymbol(const Symbol* symbol)
  56. : m_ptr(symbol)
  57. {
  58. set_symbol_flag();
  59. }
  60. StringOrSymbol(const StringOrSymbol& other)
  61. {
  62. m_ptr = other.m_ptr;
  63. if (is_string())
  64. reinterpret_cast<const StringImpl*>(m_ptr)->ref();
  65. }
  66. ALWAYS_INLINE bool is_valid() const { return m_ptr != nullptr; }
  67. ALWAYS_INLINE bool is_symbol() const { return is_valid() && (bits() & 1ul); }
  68. ALWAYS_INLINE bool is_string() const { return is_valid() && !(bits() & 1ul); }
  69. ALWAYS_INLINE String as_string() const
  70. {
  71. ASSERT(is_string());
  72. return reinterpret_cast<const StringImpl*>(m_ptr);
  73. }
  74. ALWAYS_INLINE const Symbol* as_symbol() const
  75. {
  76. ASSERT(is_symbol());
  77. return reinterpret_cast<const Symbol*>(bits() & ~1ul);
  78. }
  79. String to_display_string() const
  80. {
  81. if (is_string())
  82. return as_string();
  83. if (is_symbol())
  84. return as_symbol()->to_string();
  85. ASSERT_NOT_REACHED();
  86. }
  87. Value to_value(Interpreter& interpreter) const
  88. {
  89. if (is_string())
  90. return js_string(interpreter, as_string());
  91. if (is_symbol())
  92. return const_cast<Symbol*>(as_symbol());
  93. return {};
  94. }
  95. void visit_children(Cell::Visitor& visitor)
  96. {
  97. if (is_symbol())
  98. visitor.visit(const_cast<Symbol*>(as_symbol()));
  99. }
  100. ALWAYS_INLINE bool operator==(const StringOrSymbol& other) const
  101. {
  102. if (is_string())
  103. return other.is_string() && as_string() == other.as_string();
  104. if (is_symbol())
  105. return other.is_symbol() && as_symbol() == other.as_symbol();
  106. return true;
  107. }
  108. StringOrSymbol& operator=(const StringOrSymbol& other)
  109. {
  110. if (this == &other)
  111. return *this;
  112. m_ptr = other.m_ptr;
  113. if (is_string())
  114. reinterpret_cast<const StringImpl*>(m_ptr)->ref();
  115. return *this;
  116. }
  117. private:
  118. ALWAYS_INLINE u64 bits() const
  119. {
  120. return reinterpret_cast<uintptr_t>(m_ptr);
  121. }
  122. ALWAYS_INLINE void set_symbol_flag()
  123. {
  124. m_ptr = reinterpret_cast<const void*>(bits() | 1ul);
  125. }
  126. const void* m_ptr { nullptr };
  127. };
  128. }
  129. template<>
  130. struct AK::Traits<JS::StringOrSymbol> : public GenericTraits<JS::StringOrSymbol> {
  131. static unsigned hash(const JS::StringOrSymbol& key)
  132. {
  133. if (key.is_string())
  134. return key.as_string().hash();
  135. if (key.is_symbol())
  136. return ptr_hash(key.as_symbol());
  137. return 0;
  138. }
  139. };