PropertyName.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_i32() >= 0)
  29. return value.as_i32();
  30. auto string = value.to_string(global_object);
  31. if (string.is_null())
  32. return {};
  33. return string;
  34. }
  35. PropertyName() { }
  36. PropertyName(i32 index)
  37. : m_type(Type::Number)
  38. , m_number(index)
  39. {
  40. VERIFY(index >= 0);
  41. }
  42. PropertyName(char const* chars)
  43. : m_type(Type::String)
  44. , m_string(FlyString(chars))
  45. {
  46. }
  47. PropertyName(String const& string)
  48. : m_type(Type::String)
  49. , m_string(FlyString(string))
  50. {
  51. VERIFY(!string.is_null());
  52. }
  53. PropertyName(FlyString const& string, StringMayBeNumber string_may_be_number = StringMayBeNumber::Yes)
  54. : m_type(Type::String)
  55. , m_string_may_be_number(string_may_be_number == StringMayBeNumber::Yes)
  56. , m_string(string)
  57. {
  58. VERIFY(!string.is_null());
  59. }
  60. PropertyName(Symbol* symbol)
  61. : m_type(Type::Symbol)
  62. , m_symbol(symbol)
  63. {
  64. VERIFY(symbol);
  65. }
  66. PropertyName(StringOrSymbol const& string_or_symbol)
  67. {
  68. if (string_or_symbol.is_string()) {
  69. m_string = string_or_symbol.as_string();
  70. m_type = Type::String;
  71. } else if (string_or_symbol.is_symbol()) {
  72. m_symbol = const_cast<Symbol*>(string_or_symbol.as_symbol());
  73. m_type = Type::Symbol;
  74. }
  75. }
  76. ALWAYS_INLINE Type type() const { return m_type; }
  77. bool is_valid() const { return m_type != Type::Invalid; }
  78. bool is_number() const
  79. {
  80. if (m_type == Type::Number)
  81. return true;
  82. if (m_type != Type::String || !m_string_may_be_number)
  83. return false;
  84. return const_cast<PropertyName*>(this)->try_coerce_into_number();
  85. }
  86. bool is_string() const
  87. {
  88. if (m_type != Type::String)
  89. return false;
  90. if (!m_string_may_be_number)
  91. return true;
  92. return !const_cast<PropertyName*>(this)->try_coerce_into_number();
  93. }
  94. bool is_symbol() const { return m_type == Type::Symbol; }
  95. bool try_coerce_into_number()
  96. {
  97. VERIFY(m_string_may_be_number);
  98. if (m_string.is_empty()) {
  99. m_string_may_be_number = false;
  100. return false;
  101. }
  102. if (char first = m_string.characters()[0]; first < '0' || first > '9') {
  103. m_string_may_be_number = false;
  104. return false;
  105. } else if (m_string.length() > 1 && first == '0') {
  106. m_string_may_be_number = false;
  107. return false;
  108. }
  109. i32 property_index = m_string.to_int(TrimWhitespace::No).value_or(-1);
  110. if (property_index < 0) {
  111. m_string_may_be_number = false;
  112. return false;
  113. }
  114. m_type = Type::Number;
  115. m_number = property_index;
  116. return true;
  117. }
  118. u32 as_number() const
  119. {
  120. VERIFY(is_number());
  121. return m_number;
  122. }
  123. FlyString const& as_string() const
  124. {
  125. VERIFY(is_string());
  126. return m_string;
  127. }
  128. Symbol const* as_symbol() const
  129. {
  130. VERIFY(is_symbol());
  131. return m_symbol;
  132. }
  133. String to_string() const
  134. {
  135. VERIFY(is_valid());
  136. VERIFY(!is_symbol());
  137. if (is_string())
  138. return as_string();
  139. return String::number(as_number());
  140. }
  141. StringOrSymbol to_string_or_symbol() const
  142. {
  143. VERIFY(is_valid());
  144. VERIFY(!is_number());
  145. if (is_string())
  146. return StringOrSymbol(as_string());
  147. return StringOrSymbol(as_symbol());
  148. }
  149. Value to_value(VM& vm) const
  150. {
  151. if (is_string())
  152. return js_string(vm, m_string);
  153. if (is_number())
  154. return Value(m_number);
  155. if (is_symbol())
  156. return m_symbol;
  157. return js_undefined();
  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& value)
  197. {
  198. Formatter<StringView>::format(builder, value.to_string());
  199. }
  200. };
  201. }