Value.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Assertions.h>
  9. #include <AK/BitCast.h>
  10. #include <AK/Format.h>
  11. #include <AK/Forward.h>
  12. #include <AK/String.h>
  13. #include <AK/Types.h>
  14. #include <LibJS/Forward.h>
  15. #include <math.h>
  16. // 2 ** 53 - 1
  17. static constexpr double MAX_ARRAY_LIKE_INDEX = 9007199254740991.0;
  18. // Unique bit representation of negative zero (only sign bit set)
  19. static constexpr u64 NEGATIVE_ZERO_BITS = ((u64)1 << 63);
  20. namespace JS {
  21. class Value {
  22. public:
  23. enum class Type {
  24. Empty,
  25. Undefined,
  26. Null,
  27. Int32,
  28. Double,
  29. String,
  30. Object,
  31. Boolean,
  32. Symbol,
  33. Accessor,
  34. BigInt,
  35. NativeProperty,
  36. };
  37. enum class PreferredType {
  38. Default,
  39. String,
  40. Number,
  41. };
  42. bool is_empty() const { return m_type == Type::Empty; }
  43. bool is_undefined() const { return m_type == Type::Undefined; }
  44. bool is_null() const { return m_type == Type::Null; }
  45. bool is_number() const { return m_type == Type::Int32 || m_type == Type::Double; }
  46. bool is_string() const { return m_type == Type::String; }
  47. bool is_object() const { return m_type == Type::Object; }
  48. bool is_boolean() const { return m_type == Type::Boolean; }
  49. bool is_symbol() const { return m_type == Type::Symbol; }
  50. bool is_accessor() const { return m_type == Type::Accessor; };
  51. bool is_bigint() const { return m_type == Type::BigInt; };
  52. bool is_native_property() const { return m_type == Type::NativeProperty; }
  53. bool is_nullish() const { return is_null() || is_undefined(); }
  54. bool is_cell() const { return is_string() || is_accessor() || is_object() || is_bigint() || is_symbol() || is_native_property(); }
  55. bool is_array(GlobalObject&) const;
  56. bool is_function() const;
  57. bool is_constructor() const;
  58. bool is_regexp(GlobalObject&) const;
  59. bool is_nan() const { return is_number() && __builtin_isnan(as_double()); }
  60. bool is_infinity() const { return is_number() && __builtin_isinf(as_double()); }
  61. bool is_positive_infinity() const { return is_number() && __builtin_isinf_sign(as_double()) > 0; }
  62. bool is_negative_infinity() const { return is_number() && __builtin_isinf_sign(as_double()) < 0; }
  63. bool is_positive_zero() const { return is_number() && bit_cast<u64>(as_double()) == 0; }
  64. bool is_negative_zero() const { return is_number() && bit_cast<u64>(as_double()) == NEGATIVE_ZERO_BITS; }
  65. bool is_integer() const { return is_finite_number() && (i32)as_double() == as_double(); }
  66. bool is_finite_number() const
  67. {
  68. if (!is_number())
  69. return false;
  70. auto number = as_double();
  71. return !__builtin_isnan(number) && !__builtin_isinf(number);
  72. }
  73. Value()
  74. : m_type(Type::Empty)
  75. {
  76. }
  77. explicit Value(bool value)
  78. : m_type(Type::Boolean)
  79. {
  80. m_value.as_bool = value;
  81. }
  82. explicit Value(double value)
  83. {
  84. bool is_negative_zero = bit_cast<u64>(value) == NEGATIVE_ZERO_BITS;
  85. if (value >= NumericLimits<i32>::min() && value <= NumericLimits<i32>::max() && trunc(value) == value && !is_negative_zero) {
  86. m_type = Type::Int32;
  87. m_value.as_i32 = static_cast<i32>(value);
  88. } else {
  89. m_type = Type::Double;
  90. m_value.as_double = value;
  91. }
  92. }
  93. explicit Value(unsigned long value)
  94. {
  95. if (value > NumericLimits<i32>::max()) {
  96. m_value.as_double = static_cast<double>(value);
  97. m_type = Type::Double;
  98. } else {
  99. m_value.as_i32 = static_cast<i32>(value);
  100. m_type = Type::Int32;
  101. }
  102. }
  103. explicit Value(unsigned value)
  104. {
  105. if (value > NumericLimits<i32>::max()) {
  106. m_value.as_double = static_cast<double>(value);
  107. m_type = Type::Double;
  108. } else {
  109. m_value.as_i32 = static_cast<i32>(value);
  110. m_type = Type::Int32;
  111. }
  112. }
  113. explicit Value(i32 value)
  114. : m_type(Type::Int32)
  115. {
  116. m_value.as_i32 = value;
  117. }
  118. Value(const Object* object)
  119. : m_type(object ? Type::Object : Type::Null)
  120. {
  121. m_value.as_object = const_cast<Object*>(object);
  122. }
  123. Value(const PrimitiveString* string)
  124. : m_type(Type::String)
  125. {
  126. m_value.as_string = const_cast<PrimitiveString*>(string);
  127. }
  128. Value(const Symbol* symbol)
  129. : m_type(Type::Symbol)
  130. {
  131. m_value.as_symbol = const_cast<Symbol*>(symbol);
  132. }
  133. Value(const Accessor* accessor)
  134. : m_type(Type::Accessor)
  135. {
  136. m_value.as_accessor = const_cast<Accessor*>(accessor);
  137. }
  138. Value(const BigInt* bigint)
  139. : m_type(Type::BigInt)
  140. {
  141. m_value.as_bigint = const_cast<BigInt*>(bigint);
  142. }
  143. Value(const NativeProperty* native_property)
  144. : m_type(Type::NativeProperty)
  145. {
  146. m_value.as_native_property = const_cast<NativeProperty*>(native_property);
  147. }
  148. explicit Value(Type type)
  149. : m_type(type)
  150. {
  151. }
  152. Type type() const { return m_type; }
  153. double as_double() const
  154. {
  155. VERIFY(is_number());
  156. if (m_type == Type::Int32)
  157. return m_value.as_i32;
  158. return m_value.as_double;
  159. }
  160. bool as_bool() const
  161. {
  162. VERIFY(type() == Type::Boolean);
  163. return m_value.as_bool;
  164. }
  165. Object& as_object()
  166. {
  167. VERIFY(type() == Type::Object);
  168. return *m_value.as_object;
  169. }
  170. const Object& as_object() const
  171. {
  172. VERIFY(type() == Type::Object);
  173. return *m_value.as_object;
  174. }
  175. PrimitiveString& as_string()
  176. {
  177. VERIFY(is_string());
  178. return *m_value.as_string;
  179. }
  180. const PrimitiveString& as_string() const
  181. {
  182. VERIFY(is_string());
  183. return *m_value.as_string;
  184. }
  185. Symbol& as_symbol()
  186. {
  187. VERIFY(is_symbol());
  188. return *m_value.as_symbol;
  189. }
  190. const Symbol& as_symbol() const
  191. {
  192. VERIFY(is_symbol());
  193. return *m_value.as_symbol;
  194. }
  195. Cell& as_cell()
  196. {
  197. VERIFY(is_cell());
  198. return *m_value.as_cell;
  199. }
  200. Accessor& as_accessor()
  201. {
  202. VERIFY(is_accessor());
  203. return *m_value.as_accessor;
  204. }
  205. BigInt& as_bigint()
  206. {
  207. VERIFY(is_bigint());
  208. return *m_value.as_bigint;
  209. }
  210. NativeProperty& as_native_property()
  211. {
  212. VERIFY(is_native_property());
  213. return *m_value.as_native_property;
  214. }
  215. Array& as_array();
  216. Function& as_function();
  217. i32 as_i32() const;
  218. u32 as_u32() const;
  219. u64 encoded() const { return m_value.encoded; }
  220. String to_string(GlobalObject&, bool legacy_null_to_empty_string = false) const;
  221. PrimitiveString* to_primitive_string(GlobalObject&);
  222. Value to_primitive(GlobalObject&, PreferredType preferred_type = PreferredType::Default) const;
  223. Object* to_object(GlobalObject&) const;
  224. Value to_numeric(GlobalObject&) const;
  225. Value to_number(GlobalObject&) const;
  226. BigInt* to_bigint(GlobalObject&) const;
  227. double to_double(GlobalObject&) const;
  228. StringOrSymbol to_property_key(GlobalObject&) const;
  229. i32 to_i32(GlobalObject& global_object) const
  230. {
  231. if (m_type == Type::Int32)
  232. return m_value.as_i32;
  233. return to_i32_slow_case(global_object);
  234. }
  235. u32 to_u32(GlobalObject&) const;
  236. size_t to_length(GlobalObject&) const;
  237. size_t to_index(GlobalObject&) const;
  238. double to_integer_or_infinity(GlobalObject&) const;
  239. bool to_boolean() const;
  240. String to_string_without_side_effects() const;
  241. Value value_or(Value fallback) const
  242. {
  243. if (is_empty())
  244. return fallback;
  245. return *this;
  246. }
  247. String typeof() const;
  248. private:
  249. Type m_type { Type::Empty };
  250. i32 to_i32_slow_case(GlobalObject&) const;
  251. union {
  252. bool as_bool;
  253. i32 as_i32;
  254. double as_double;
  255. PrimitiveString* as_string;
  256. Symbol* as_symbol;
  257. Object* as_object;
  258. Cell* as_cell;
  259. Accessor* as_accessor;
  260. BigInt* as_bigint;
  261. NativeProperty* as_native_property;
  262. u64 encoded;
  263. } m_value { .encoded = 0 };
  264. };
  265. inline Value js_undefined()
  266. {
  267. return Value(Value::Type::Undefined);
  268. }
  269. inline Value js_null()
  270. {
  271. return Value(Value::Type::Null);
  272. }
  273. inline Value js_nan()
  274. {
  275. return Value(NAN);
  276. }
  277. inline Value js_infinity()
  278. {
  279. return Value(INFINITY);
  280. }
  281. inline Value js_negative_infinity()
  282. {
  283. return Value(-INFINITY);
  284. }
  285. inline void Cell::Visitor::visit(Value value)
  286. {
  287. if (value.is_cell())
  288. visit_impl(value.as_cell());
  289. }
  290. Value greater_than(GlobalObject&, Value lhs, Value rhs);
  291. Value greater_than_equals(GlobalObject&, Value lhs, Value rhs);
  292. Value less_than(GlobalObject&, Value lhs, Value rhs);
  293. Value less_than_equals(GlobalObject&, Value lhs, Value rhs);
  294. Value bitwise_and(GlobalObject&, Value lhs, Value rhs);
  295. Value bitwise_or(GlobalObject&, Value lhs, Value rhs);
  296. Value bitwise_xor(GlobalObject&, Value lhs, Value rhs);
  297. Value bitwise_not(GlobalObject&, Value);
  298. Value unary_plus(GlobalObject&, Value);
  299. Value unary_minus(GlobalObject&, Value);
  300. Value left_shift(GlobalObject&, Value lhs, Value rhs);
  301. Value right_shift(GlobalObject&, Value lhs, Value rhs);
  302. Value unsigned_right_shift(GlobalObject&, Value lhs, Value rhs);
  303. Value add(GlobalObject&, Value lhs, Value rhs);
  304. Value sub(GlobalObject&, Value lhs, Value rhs);
  305. Value mul(GlobalObject&, Value lhs, Value rhs);
  306. Value div(GlobalObject&, Value lhs, Value rhs);
  307. Value mod(GlobalObject&, Value lhs, Value rhs);
  308. Value exp(GlobalObject&, Value lhs, Value rhs);
  309. Value in(GlobalObject&, Value lhs, Value rhs);
  310. Value instance_of(GlobalObject&, Value lhs, Value rhs);
  311. Value ordinary_has_instance(GlobalObject&, Value lhs, Value rhs);
  312. bool abstract_eq(GlobalObject&, Value lhs, Value rhs);
  313. bool strict_eq(Value lhs, Value rhs);
  314. bool same_value(Value lhs, Value rhs);
  315. bool same_value_zero(Value lhs, Value rhs);
  316. bool same_value_non_numeric(Value lhs, Value rhs);
  317. TriState abstract_relation(GlobalObject&, bool left_first, Value lhs, Value rhs);
  318. Function* get_method(GlobalObject& global_object, Value, const PropertyName&);
  319. size_t length_of_array_like(GlobalObject&, const Object&);
  320. Object* species_constructor(GlobalObject&, const Object&, Object& default_constructor);
  321. Value require_object_coercible(GlobalObject&, Value);
  322. }
  323. namespace AK {
  324. template<>
  325. struct Formatter<JS::Value> : Formatter<StringView> {
  326. void format(FormatBuilder& builder, const JS::Value& value)
  327. {
  328. Formatter<StringView>::format(builder, value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
  329. }
  330. };
  331. }