Value.h 10 KB

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