PropertyKey.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/StringOrSymbol.h>
  10. namespace JS {
  11. class PropertyKey {
  12. public:
  13. enum class Type : u8 {
  14. Invalid,
  15. Number,
  16. String,
  17. Symbol,
  18. };
  19. enum class StringMayBeNumber {
  20. Yes,
  21. No,
  22. };
  23. static PropertyKey from_value(GlobalObject& global_object, Value value)
  24. {
  25. if (value.is_empty())
  26. return {};
  27. if (value.is_symbol())
  28. return value.as_symbol();
  29. if (value.is_integral_number() && value.as_double() >= 0 && value.as_double() < NumericLimits<u32>::max())
  30. return value.as_u32();
  31. return TRY_OR_DISCARD(value.to_string(global_object));
  32. }
  33. PropertyKey() { }
  34. template<Integral T>
  35. PropertyKey(T index)
  36. {
  37. // FIXME: Replace this with requires(IsUnsigned<T>)?
  38. // Needs changes in various places using `int` (but not actually being in the negative range)
  39. VERIFY(index >= 0);
  40. if constexpr (NumericLimits<T>::max() >= NumericLimits<u32>::max()) {
  41. if (index >= NumericLimits<u32>::max()) {
  42. m_string = String::number(index);
  43. m_type = Type::String;
  44. m_string_may_be_number = false;
  45. return;
  46. }
  47. }
  48. m_type = Type::Number;
  49. m_number = index;
  50. }
  51. PropertyKey(char const* chars)
  52. : m_type(Type::String)
  53. , m_string(FlyString(chars))
  54. {
  55. }
  56. PropertyKey(String const& string)
  57. : m_type(Type::String)
  58. , m_string(FlyString(string))
  59. {
  60. VERIFY(!m_string.is_null());
  61. }
  62. PropertyKey(FlyString string, StringMayBeNumber string_may_be_number = StringMayBeNumber::Yes)
  63. : m_string_may_be_number(string_may_be_number == StringMayBeNumber::Yes)
  64. , m_type(Type::String)
  65. , m_string(move(string))
  66. {
  67. VERIFY(!m_string.is_null());
  68. }
  69. PropertyKey(Symbol& symbol)
  70. : m_type(Type::Symbol)
  71. , m_symbol(&symbol)
  72. {
  73. }
  74. PropertyKey(StringOrSymbol const& string_or_symbol)
  75. {
  76. if (string_or_symbol.is_string()) {
  77. m_string = string_or_symbol.as_string();
  78. m_type = Type::String;
  79. } else if (string_or_symbol.is_symbol()) {
  80. m_symbol = const_cast<Symbol*>(string_or_symbol.as_symbol());
  81. m_type = Type::Symbol;
  82. }
  83. }
  84. ALWAYS_INLINE Type type() const { return m_type; }
  85. bool is_valid() const { return m_type != Type::Invalid; }
  86. bool is_number() const
  87. {
  88. if (m_type == Type::Number)
  89. return true;
  90. if (m_type != Type::String || !m_string_may_be_number)
  91. return false;
  92. return const_cast<PropertyKey*>(this)->try_coerce_into_number();
  93. }
  94. bool is_string() const
  95. {
  96. if (m_type != Type::String)
  97. return false;
  98. if (!m_string_may_be_number)
  99. return true;
  100. return !const_cast<PropertyKey*>(this)->try_coerce_into_number();
  101. }
  102. bool is_symbol() const { return m_type == Type::Symbol; }
  103. bool try_coerce_into_number()
  104. {
  105. VERIFY(m_string_may_be_number);
  106. if (m_string.is_empty()) {
  107. m_string_may_be_number = false;
  108. return false;
  109. }
  110. if (char first = m_string.characters()[0]; first < '0' || first > '9') {
  111. m_string_may_be_number = false;
  112. return false;
  113. } else if (m_string.length() > 1 && first == '0') {
  114. m_string_may_be_number = false;
  115. return false;
  116. }
  117. auto property_index = m_string.to_uint(TrimWhitespace::No);
  118. if (!property_index.has_value() || property_index.value() == NumericLimits<u32>::max()) {
  119. m_string_may_be_number = false;
  120. return false;
  121. }
  122. m_type = Type::Number;
  123. m_number = *property_index;
  124. return true;
  125. }
  126. u32 as_number() const
  127. {
  128. VERIFY(is_number());
  129. return m_number;
  130. }
  131. FlyString const& as_string() const
  132. {
  133. VERIFY(is_string());
  134. return m_string;
  135. }
  136. Symbol const* as_symbol() const
  137. {
  138. VERIFY(is_symbol());
  139. return m_symbol;
  140. }
  141. String to_string() const
  142. {
  143. VERIFY(is_valid());
  144. VERIFY(!is_symbol());
  145. if (is_string())
  146. return as_string();
  147. return String::number(as_number());
  148. }
  149. StringOrSymbol to_string_or_symbol() const
  150. {
  151. VERIFY(is_valid());
  152. VERIFY(!is_number());
  153. if (is_string())
  154. return StringOrSymbol(as_string());
  155. return StringOrSymbol(as_symbol());
  156. }
  157. private:
  158. bool m_string_may_be_number { true };
  159. Type m_type { Type::Invalid };
  160. u32 m_number { 0 };
  161. FlyString m_string;
  162. Symbol* m_symbol { nullptr };
  163. };
  164. }
  165. namespace AK {
  166. template<>
  167. struct Traits<JS::PropertyKey> : public GenericTraits<JS::PropertyKey> {
  168. static unsigned hash(JS::PropertyKey const& name)
  169. {
  170. VERIFY(name.is_valid());
  171. if (name.is_string())
  172. return name.as_string().hash();
  173. if (name.is_number())
  174. return int_hash(name.as_number());
  175. return ptr_hash(name.as_symbol());
  176. }
  177. static bool equals(JS::PropertyKey const& a, JS::PropertyKey const& b)
  178. {
  179. if (a.type() != b.type())
  180. return false;
  181. switch (a.type()) {
  182. case JS::PropertyKey::Type::Number:
  183. return a.as_number() == b.as_number();
  184. case JS::PropertyKey::Type::String:
  185. return a.as_string() == b.as_string();
  186. case JS::PropertyKey::Type::Symbol:
  187. return a.as_symbol() == b.as_symbol();
  188. default:
  189. VERIFY_NOT_REACHED();
  190. }
  191. }
  192. };
  193. template<>
  194. struct Formatter<JS::PropertyKey> : Formatter<StringView> {
  195. void format(FormatBuilder& builder, JS::PropertyKey const& property_name)
  196. {
  197. if (!property_name.is_valid())
  198. Formatter<StringView>::format(builder, "<invalid PropertyKey>");
  199. else if (property_name.is_number())
  200. Formatter<StringView>::format(builder, String::number(property_name.as_number()));
  201. else
  202. Formatter<StringView>::format(builder, property_name.to_string_or_symbol().to_display_string());
  203. }
  204. };
  205. }