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