Value.h 13 KB

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