PropertyKey.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_byte_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 = ByteString::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(ByteString const& string)
  58. : m_type(Type::String)
  59. , m_string(DeprecatedFlyString(string))
  60. {
  61. }
  62. PropertyKey(DeprecatedFlyString 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. }
  68. PropertyKey(NonnullGCPtr<Symbol> symbol)
  69. : m_type(Type::Symbol)
  70. , m_symbol(symbol)
  71. {
  72. }
  73. PropertyKey(StringOrSymbol const& string_or_symbol)
  74. {
  75. if (string_or_symbol.is_string()) {
  76. m_string = string_or_symbol.as_string();
  77. m_type = Type::String;
  78. } else if (string_or_symbol.is_symbol()) {
  79. m_symbol = const_cast<Symbol*>(string_or_symbol.as_symbol());
  80. m_type = Type::Symbol;
  81. }
  82. }
  83. ALWAYS_INLINE Type type() const { return m_type; }
  84. bool is_valid() const { return m_type != Type::Invalid; }
  85. bool is_number() const
  86. {
  87. if (m_type == Type::Number)
  88. return true;
  89. if (m_type != Type::String || !m_string_may_be_number)
  90. return false;
  91. return const_cast<PropertyKey*>(this)->try_coerce_into_number();
  92. }
  93. bool is_string() const
  94. {
  95. if (m_type != Type::String)
  96. return false;
  97. if (!m_string_may_be_number)
  98. return true;
  99. return !const_cast<PropertyKey*>(this)->try_coerce_into_number();
  100. }
  101. bool is_symbol() const { return m_type == Type::Symbol; }
  102. bool try_coerce_into_number()
  103. {
  104. VERIFY(m_string_may_be_number);
  105. if (m_string.is_empty()) {
  106. m_string_may_be_number = false;
  107. return false;
  108. }
  109. if (char first = m_string.characters()[0]; first < '0' || first > '9') {
  110. m_string_may_be_number = false;
  111. return false;
  112. } else if (m_string.length() > 1 && first == '0') {
  113. m_string_may_be_number = false;
  114. return false;
  115. }
  116. auto property_index = m_string.to_number<unsigned>(TrimWhitespace::No);
  117. if (!property_index.has_value() || property_index.value() == NumericLimits<u32>::max()) {
  118. m_string_may_be_number = false;
  119. return false;
  120. }
  121. m_type = Type::Number;
  122. m_number = *property_index;
  123. return true;
  124. }
  125. u32 as_number() const
  126. {
  127. VERIFY(is_number());
  128. return m_number;
  129. }
  130. DeprecatedFlyString const& as_string() const
  131. {
  132. VERIFY(is_string());
  133. return m_string;
  134. }
  135. Symbol const* as_symbol() const
  136. {
  137. VERIFY(is_symbol());
  138. return m_symbol;
  139. }
  140. ByteString to_string() const
  141. {
  142. VERIFY(is_valid());
  143. VERIFY(!is_symbol());
  144. if (is_string())
  145. return as_string();
  146. return ByteString::number(as_number());
  147. }
  148. StringOrSymbol to_string_or_symbol() const
  149. {
  150. VERIFY(is_valid());
  151. VERIFY(!is_number());
  152. if (is_string())
  153. return StringOrSymbol(as_string());
  154. return StringOrSymbol(as_symbol());
  155. }
  156. private:
  157. bool m_string_may_be_number { true };
  158. Type m_type { Type::Invalid };
  159. u32 m_number { 0 };
  160. DeprecatedFlyString m_string;
  161. Handle<Symbol> m_symbol;
  162. };
  163. }
  164. namespace AK {
  165. template<>
  166. struct Traits<JS::PropertyKey> : public DefaultTraits<JS::PropertyKey> {
  167. static unsigned hash(JS::PropertyKey const& name)
  168. {
  169. VERIFY(name.is_valid());
  170. if (name.is_string())
  171. return name.as_string().hash();
  172. if (name.is_number())
  173. return int_hash(name.as_number());
  174. return ptr_hash(name.as_symbol());
  175. }
  176. static bool equals(JS::PropertyKey const& a, JS::PropertyKey const& b)
  177. {
  178. if (a.type() != b.type())
  179. return false;
  180. switch (a.type()) {
  181. case JS::PropertyKey::Type::Number:
  182. return a.as_number() == b.as_number();
  183. case JS::PropertyKey::Type::String:
  184. return a.as_string() == b.as_string();
  185. case JS::PropertyKey::Type::Symbol:
  186. return a.as_symbol() == b.as_symbol();
  187. default:
  188. VERIFY_NOT_REACHED();
  189. }
  190. }
  191. };
  192. template<>
  193. struct Formatter<JS::PropertyKey> : Formatter<StringView> {
  194. ErrorOr<void> format(FormatBuilder& builder, JS::PropertyKey const& property_key)
  195. {
  196. if (!property_key.is_valid())
  197. return builder.put_string("<invalid PropertyKey>"sv);
  198. if (property_key.is_number())
  199. return builder.put_u64(property_key.as_number());
  200. return builder.put_string(property_key.to_string_or_symbol().to_display_string());
  201. }
  202. };
  203. }