PropertyName.h 6.3 KB

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