PropertyKey.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedFlyString.h>
  8. #include <AK/FlyString.h>
  9. #include <LibGC/Root.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibJS/Runtime/StringOrSymbol.h>
  12. namespace JS {
  13. class PropertyKey {
  14. public:
  15. enum class Type : u8 {
  16. Invalid,
  17. Number,
  18. String,
  19. Symbol,
  20. };
  21. enum class StringMayBeNumber {
  22. Yes,
  23. No,
  24. };
  25. static ThrowCompletionOr<PropertyKey> from_value(VM& vm, Value value)
  26. {
  27. VERIFY(!value.is_empty());
  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() = delete;
  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(FlyString const& string)
  63. : m_type(Type::String)
  64. , m_string(string.to_deprecated_fly_string())
  65. {
  66. }
  67. PropertyKey(DeprecatedFlyString string, StringMayBeNumber string_may_be_number = StringMayBeNumber::Yes)
  68. : m_string_may_be_number(string_may_be_number == StringMayBeNumber::Yes)
  69. , m_type(Type::String)
  70. , m_string(move(string))
  71. {
  72. }
  73. PropertyKey(GC::Ref<Symbol> symbol)
  74. : m_type(Type::Symbol)
  75. , m_symbol(symbol)
  76. {
  77. }
  78. PropertyKey(StringOrSymbol const& string_or_symbol)
  79. {
  80. if (string_or_symbol.is_string()) {
  81. m_string = string_or_symbol.as_string();
  82. m_type = Type::String;
  83. } else if (string_or_symbol.is_symbol()) {
  84. m_symbol = const_cast<Symbol*>(string_or_symbol.as_symbol());
  85. m_type = Type::Symbol;
  86. }
  87. }
  88. ALWAYS_INLINE Type type() const { return m_type; }
  89. bool is_number() const
  90. {
  91. if (m_type == Type::Number)
  92. return true;
  93. if (m_type != Type::String || !m_string_may_be_number)
  94. return false;
  95. return const_cast<PropertyKey*>(this)->try_coerce_into_number();
  96. }
  97. bool is_string() const
  98. {
  99. if (m_type != Type::String)
  100. return false;
  101. if (!m_string_may_be_number)
  102. return true;
  103. return !const_cast<PropertyKey*>(this)->try_coerce_into_number();
  104. }
  105. bool is_symbol() const { return m_type == Type::Symbol; }
  106. bool try_coerce_into_number()
  107. {
  108. VERIFY(m_string_may_be_number);
  109. if (m_string.is_empty()) {
  110. m_string_may_be_number = false;
  111. return false;
  112. }
  113. if (char first = m_string.characters()[0]; first < '0' || first > '9') {
  114. m_string_may_be_number = false;
  115. return false;
  116. } else if (m_string.length() > 1 && first == '0') {
  117. m_string_may_be_number = false;
  118. return false;
  119. }
  120. auto property_index = m_string.to_number<unsigned>(TrimWhitespace::No);
  121. if (!property_index.has_value() || property_index.value() == NumericLimits<u32>::max()) {
  122. m_string_may_be_number = false;
  123. return false;
  124. }
  125. m_type = Type::Number;
  126. m_number = *property_index;
  127. return true;
  128. }
  129. u32 as_number() const
  130. {
  131. VERIFY(is_number());
  132. return m_number;
  133. }
  134. DeprecatedFlyString const& as_string() const
  135. {
  136. VERIFY(is_string());
  137. return m_string;
  138. }
  139. Symbol const* as_symbol() const
  140. {
  141. VERIFY(is_symbol());
  142. return m_symbol;
  143. }
  144. ByteString to_string() const
  145. {
  146. VERIFY(!is_symbol());
  147. if (is_string())
  148. return as_string();
  149. return ByteString::number(as_number());
  150. }
  151. StringOrSymbol to_string_or_symbol() const
  152. {
  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. GC::Root<Symbol> m_symbol;
  164. };
  165. }
  166. namespace AK {
  167. template<>
  168. struct Traits<JS::PropertyKey> : public DefaultTraits<JS::PropertyKey> {
  169. static unsigned hash(JS::PropertyKey const& name)
  170. {
  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. ErrorOr<void> format(FormatBuilder& builder, JS::PropertyKey const& property_key)
  196. {
  197. if (property_key.is_number())
  198. return builder.put_u64(property_key.as_number());
  199. return builder.put_string(property_key.to_string_or_symbol().to_display_string());
  200. }
  201. };
  202. }