Value.h 11 KB

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