Value.h 11 KB

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