Value.h 11 KB

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