Value.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Assertions.h>
  10. #include <AK/BitCast.h>
  11. #include <AK/ByteString.h>
  12. #include <AK/Format.h>
  13. #include <AK/Forward.h>
  14. #include <AK/Function.h>
  15. #include <AK/Result.h>
  16. #include <AK/SourceLocation.h>
  17. #include <AK/String.h>
  18. #include <AK/Types.h>
  19. #include <LibGC/NanBoxedValue.h>
  20. #include <LibGC/Ptr.h>
  21. #include <LibGC/Root.h>
  22. #include <LibJS/Forward.h>
  23. #include <LibJS/Heap/Cell.h>
  24. #include <math.h>
  25. namespace JS {
  26. // 2 ** 53 - 1
  27. static constexpr double MAX_ARRAY_LIKE_INDEX = 9007199254740991.0;
  28. // Unique bit representation of negative zero (only sign bit set)
  29. static constexpr u64 NEGATIVE_ZERO_BITS = ((u64)1 << 63);
  30. // This leaves us 3 bits to tag the type of pointer:
  31. static constexpr u64 OBJECT_TAG = 0b001 | GC::IS_CELL_BIT;
  32. static constexpr u64 STRING_TAG = 0b010 | GC::IS_CELL_BIT;
  33. static constexpr u64 SYMBOL_TAG = 0b011 | GC::IS_CELL_BIT;
  34. static constexpr u64 ACCESSOR_TAG = 0b100 | GC::IS_CELL_BIT;
  35. static constexpr u64 BIGINT_TAG = 0b101 | GC::IS_CELL_BIT;
  36. // We can then by extracting the top 13 bits quickly check if a Value is
  37. // pointer backed.
  38. static_assert((OBJECT_TAG & GC::IS_CELL_PATTERN) == GC::IS_CELL_PATTERN);
  39. static_assert((STRING_TAG & GC::IS_CELL_PATTERN) == GC::IS_CELL_PATTERN);
  40. static_assert((GC::CANON_NAN_BITS & GC::IS_CELL_PATTERN) != GC::IS_CELL_PATTERN);
  41. static_assert((GC::NEGATIVE_INFINITY_BITS & GC::IS_CELL_PATTERN) != GC::IS_CELL_PATTERN);
  42. // Then for the non pointer backed types we don't set the sign bit and use the
  43. // three lower bits for tagging as well.
  44. static constexpr u64 UNDEFINED_TAG = 0b110 | GC::BASE_TAG;
  45. static constexpr u64 NULL_TAG = 0b111 | GC::BASE_TAG;
  46. static constexpr u64 BOOLEAN_TAG = 0b001 | GC::BASE_TAG;
  47. static constexpr u64 INT32_TAG = 0b010 | GC::BASE_TAG;
  48. static constexpr u64 EMPTY_TAG = 0b011 | GC::BASE_TAG;
  49. // Notice how only undefined and null have the top bit set, this mean we can
  50. // quickly check for nullish values by checking if the top and bottom bits are set
  51. // but the middle one isn't.
  52. static constexpr u64 IS_NULLISH_EXTRACT_PATTERN = 0xFFFEULL;
  53. static constexpr u64 IS_NULLISH_PATTERN = 0x7FFEULL;
  54. static_assert((UNDEFINED_TAG & IS_NULLISH_EXTRACT_PATTERN) == IS_NULLISH_PATTERN);
  55. static_assert((NULL_TAG & IS_NULLISH_EXTRACT_PATTERN) == IS_NULLISH_PATTERN);
  56. static_assert((BOOLEAN_TAG & IS_NULLISH_EXTRACT_PATTERN) != IS_NULLISH_PATTERN);
  57. static_assert((INT32_TAG & IS_NULLISH_EXTRACT_PATTERN) != IS_NULLISH_PATTERN);
  58. static_assert((EMPTY_TAG & IS_NULLISH_EXTRACT_PATTERN) != IS_NULLISH_PATTERN);
  59. // We also have the empty tag to represent array holes however since empty
  60. // values are not valid anywhere else we can use this "value" to our advantage
  61. // in Optional<Value> to represent the empty optional.
  62. static constexpr u64 SHIFTED_BOOLEAN_TAG = BOOLEAN_TAG << GC::TAG_SHIFT;
  63. static constexpr u64 SHIFTED_INT32_TAG = INT32_TAG << GC::TAG_SHIFT;
  64. // Summary:
  65. // To pack all the different value in to doubles we use the following schema:
  66. // s = sign, e = exponent, m = mantissa
  67. // The top part is the tag and the bottom the payload.
  68. // 0bseeeeeeeeeeemmmm mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  69. // 0b0111111111111000 0... is the only real NaN
  70. // 0b1111111111111xxx yyy... xxx = pointer type, yyy = pointer value
  71. // 0b0111111111111xxx yyy... xxx = non-pointer type, yyy = value or 0 if just type
  72. // Future expansion: We are not fully utilizing all the possible bit patterns
  73. // yet, these choices were made to make it easy to implement and understand.
  74. // We can for example drop the always 1 top bit of the mantissa expanding our
  75. // options from 8 tags to 15 but since we currently only use 5 for both sign bits
  76. // this is not needed.
  77. class Value : public GC::NanBoxedValue {
  78. public:
  79. enum class PreferredType {
  80. Default,
  81. String,
  82. Number,
  83. };
  84. [[nodiscard]] u16 tag() const { return m_value.tag; }
  85. bool is_empty() const { return m_value.tag == EMPTY_TAG; }
  86. bool is_undefined() const { return m_value.tag == UNDEFINED_TAG; }
  87. bool is_null() const { return m_value.tag == NULL_TAG; }
  88. bool is_number() const { return is_double() || is_int32(); }
  89. bool is_string() const { return m_value.tag == STRING_TAG; }
  90. bool is_object() const { return m_value.tag == OBJECT_TAG; }
  91. bool is_boolean() const { return m_value.tag == BOOLEAN_TAG; }
  92. bool is_symbol() const { return m_value.tag == SYMBOL_TAG; }
  93. bool is_accessor() const { return m_value.tag == ACCESSOR_TAG; }
  94. bool is_bigint() const { return m_value.tag == BIGINT_TAG; }
  95. bool is_nullish() const { return (m_value.tag & IS_NULLISH_EXTRACT_PATTERN) == IS_NULLISH_PATTERN; }
  96. ThrowCompletionOr<bool> is_array(VM&) const;
  97. bool is_function() const;
  98. bool is_constructor() const;
  99. bool is_error() const;
  100. ThrowCompletionOr<bool> is_regexp(VM&) const;
  101. bool is_infinity() const
  102. {
  103. static_assert(GC::NEGATIVE_INFINITY_BITS == (0x1ULL << 63 | GC::POSITIVE_INFINITY_BITS));
  104. return (0x1ULL << 63 | m_value.encoded) == GC::NEGATIVE_INFINITY_BITS;
  105. }
  106. bool is_positive_infinity() const
  107. {
  108. return m_value.encoded == GC::POSITIVE_INFINITY_BITS;
  109. }
  110. bool is_negative_infinity() const
  111. {
  112. return m_value.encoded == GC::NEGATIVE_INFINITY_BITS;
  113. }
  114. bool is_positive_zero() const
  115. {
  116. return m_value.encoded == 0 || (is_int32() && as_i32() == 0);
  117. }
  118. bool is_negative_zero() const
  119. {
  120. return m_value.encoded == NEGATIVE_ZERO_BITS;
  121. }
  122. bool is_integral_number() const
  123. {
  124. if (is_int32())
  125. return true;
  126. return is_finite_number() && trunc(as_double()) == as_double();
  127. }
  128. bool is_finite_number() const
  129. {
  130. if (!is_number())
  131. return false;
  132. if (is_int32())
  133. return true;
  134. return !is_nan() && !is_infinity();
  135. }
  136. Value()
  137. : Value(EMPTY_TAG << GC::TAG_SHIFT, (u64)0)
  138. {
  139. }
  140. template<typename T>
  141. requires(IsSameIgnoringCV<T, bool>) explicit Value(T value)
  142. : Value(BOOLEAN_TAG << GC::TAG_SHIFT, (u64)value)
  143. {
  144. }
  145. explicit Value(double value)
  146. {
  147. bool is_negative_zero = bit_cast<u64>(value) == NEGATIVE_ZERO_BITS;
  148. if (value >= NumericLimits<i32>::min() && value <= NumericLimits<i32>::max() && trunc(value) == value && !is_negative_zero) {
  149. ASSERT(!(SHIFTED_INT32_TAG & (static_cast<i32>(value) & 0xFFFFFFFFul)));
  150. m_value.encoded = SHIFTED_INT32_TAG | (static_cast<i32>(value) & 0xFFFFFFFFul);
  151. } else {
  152. if (isnan(value)) [[unlikely]]
  153. m_value.encoded = GC::CANON_NAN_BITS;
  154. else
  155. m_value.as_double = value;
  156. }
  157. }
  158. explicit Value(f16 value)
  159. : Value(static_cast<double>(value))
  160. {
  161. }
  162. // NOTE: A couple of integral types are excluded here:
  163. // - i32 has its own dedicated Value constructor
  164. // - i64 cannot safely be cast to a double
  165. // - bool isn't a number type and has its own dedicated Value constructor
  166. template<typename T>
  167. requires(IsIntegral<T> && !IsSameIgnoringCV<T, i32> && !IsSameIgnoringCV<T, i64> && !IsSameIgnoringCV<T, bool>) explicit Value(T value)
  168. {
  169. if (value > NumericLimits<i32>::max()) {
  170. m_value.as_double = static_cast<double>(value);
  171. } else {
  172. ASSERT(!(SHIFTED_INT32_TAG & (static_cast<i32>(value) & 0xFFFFFFFFul)));
  173. m_value.encoded = SHIFTED_INT32_TAG | (static_cast<i32>(value) & 0xFFFFFFFFul);
  174. }
  175. }
  176. explicit Value(unsigned value)
  177. {
  178. if (value > NumericLimits<i32>::max()) {
  179. m_value.as_double = static_cast<double>(value);
  180. } else {
  181. ASSERT(!(SHIFTED_INT32_TAG & (static_cast<i32>(value) & 0xFFFFFFFFul)));
  182. m_value.encoded = SHIFTED_INT32_TAG | (static_cast<i32>(value) & 0xFFFFFFFFul);
  183. }
  184. }
  185. explicit Value(i32 value)
  186. : Value(SHIFTED_INT32_TAG, (u32)value)
  187. {
  188. }
  189. Value(Object const* object)
  190. : Value(OBJECT_TAG << GC::TAG_SHIFT, reinterpret_cast<void const*>(object))
  191. {
  192. }
  193. Value(PrimitiveString const* string)
  194. : Value(STRING_TAG << GC::TAG_SHIFT, reinterpret_cast<void const*>(string))
  195. {
  196. }
  197. Value(Symbol const* symbol)
  198. : Value(SYMBOL_TAG << GC::TAG_SHIFT, reinterpret_cast<void const*>(symbol))
  199. {
  200. }
  201. Value(Accessor const* accessor)
  202. : Value(ACCESSOR_TAG << GC::TAG_SHIFT, reinterpret_cast<void const*>(accessor))
  203. {
  204. }
  205. Value(BigInt const* bigint)
  206. : Value(BIGINT_TAG << GC::TAG_SHIFT, reinterpret_cast<void const*>(bigint))
  207. {
  208. }
  209. template<typename T>
  210. Value(GC::Ptr<T> ptr)
  211. : Value(ptr.ptr())
  212. {
  213. }
  214. template<typename T>
  215. Value(GC::Ref<T> ptr)
  216. : Value(ptr.ptr())
  217. {
  218. }
  219. template<typename T>
  220. Value(GC::Root<T> const& ptr)
  221. : Value(ptr.ptr())
  222. {
  223. }
  224. Cell& as_cell()
  225. {
  226. VERIFY(is_cell());
  227. return *extract_pointer<Cell>();
  228. }
  229. Cell& as_cell() const
  230. {
  231. VERIFY(is_cell());
  232. return *extract_pointer<Cell>();
  233. }
  234. double as_double() const
  235. {
  236. VERIFY(is_number());
  237. if (is_int32())
  238. return as_i32();
  239. return m_value.as_double;
  240. }
  241. bool as_bool() const
  242. {
  243. VERIFY(is_boolean());
  244. return static_cast<bool>(m_value.encoded & 0x1);
  245. }
  246. Object& as_object()
  247. {
  248. VERIFY(is_object());
  249. return *extract_pointer<Object>();
  250. }
  251. Object const& as_object() const
  252. {
  253. VERIFY(is_object());
  254. return *extract_pointer<Object>();
  255. }
  256. PrimitiveString& as_string()
  257. {
  258. VERIFY(is_string());
  259. return *extract_pointer<PrimitiveString>();
  260. }
  261. PrimitiveString const& as_string() const
  262. {
  263. VERIFY(is_string());
  264. return *extract_pointer<PrimitiveString>();
  265. }
  266. Symbol& as_symbol()
  267. {
  268. VERIFY(is_symbol());
  269. return *extract_pointer<Symbol>();
  270. }
  271. Symbol const& as_symbol() const
  272. {
  273. VERIFY(is_symbol());
  274. return *extract_pointer<Symbol>();
  275. }
  276. Accessor& as_accessor()
  277. {
  278. VERIFY(is_accessor());
  279. return *extract_pointer<Accessor>();
  280. }
  281. BigInt const& as_bigint() const
  282. {
  283. VERIFY(is_bigint());
  284. return *extract_pointer<BigInt>();
  285. }
  286. BigInt& as_bigint()
  287. {
  288. VERIFY(is_bigint());
  289. return *extract_pointer<BigInt>();
  290. }
  291. Array& as_array();
  292. FunctionObject& as_function();
  293. FunctionObject const& as_function() const;
  294. u64 encoded() const { return m_value.encoded; }
  295. ThrowCompletionOr<String> to_string(VM&) const;
  296. ThrowCompletionOr<ByteString> to_byte_string(VM&) const;
  297. ThrowCompletionOr<Utf16String> to_utf16_string(VM&) const;
  298. ThrowCompletionOr<String> to_well_formed_string(VM&) const;
  299. ThrowCompletionOr<GC::Ref<PrimitiveString>> to_primitive_string(VM&);
  300. ThrowCompletionOr<Value> to_primitive(VM&, PreferredType preferred_type = PreferredType::Default) const;
  301. ThrowCompletionOr<GC::Ref<Object>> to_object(VM&) const;
  302. ThrowCompletionOr<Value> to_numeric(VM&) const;
  303. ThrowCompletionOr<Value> to_number(VM&) const;
  304. ThrowCompletionOr<GC::Ref<BigInt>> to_bigint(VM&) const;
  305. ThrowCompletionOr<i64> to_bigint_int64(VM&) const;
  306. ThrowCompletionOr<u64> to_bigint_uint64(VM&) const;
  307. ThrowCompletionOr<double> to_double(VM&) const;
  308. ThrowCompletionOr<PropertyKey> to_property_key(VM&) const;
  309. ThrowCompletionOr<i32> to_i32(VM&) const;
  310. ThrowCompletionOr<u32> to_u32(VM&) const;
  311. ThrowCompletionOr<i16> to_i16(VM&) const;
  312. ThrowCompletionOr<u16> to_u16(VM&) const;
  313. ThrowCompletionOr<i8> to_i8(VM&) const;
  314. ThrowCompletionOr<u8> to_u8(VM&) const;
  315. ThrowCompletionOr<u8> to_u8_clamp(VM&) const;
  316. ThrowCompletionOr<size_t> to_length(VM&) const;
  317. ThrowCompletionOr<size_t> to_index(VM&) const;
  318. ThrowCompletionOr<double> to_integer_or_infinity(VM&) const;
  319. bool to_boolean() const;
  320. ThrowCompletionOr<Value> get(VM&, PropertyKey const&) const;
  321. ThrowCompletionOr<GC::Ptr<FunctionObject>> get_method(VM&, PropertyKey const&) const;
  322. [[nodiscard]] String to_string_without_side_effects() const;
  323. Value value_or(Value fallback) const
  324. {
  325. if (is_empty())
  326. return fallback;
  327. return *this;
  328. }
  329. [[nodiscard]] GC::Ref<PrimitiveString> typeof_(VM&) const;
  330. bool operator==(Value const&) const;
  331. template<typename... Args>
  332. [[nodiscard]] ALWAYS_INLINE ThrowCompletionOr<Value> invoke(VM&, PropertyKey const& property_key, Args... args);
  333. // A double is any Value which does not have the full exponent and top mantissa bit set or has
  334. // exactly only those bits set.
  335. bool is_double() const { return (m_value.encoded & GC::CANON_NAN_BITS) != GC::CANON_NAN_BITS || (m_value.encoded == GC::CANON_NAN_BITS); }
  336. bool is_int32() const { return m_value.tag == INT32_TAG; }
  337. i32 as_i32() const
  338. {
  339. VERIFY(is_int32());
  340. return static_cast<i32>(m_value.encoded & 0xFFFFFFFF);
  341. }
  342. i32 as_i32_clamped_integral_number() const
  343. {
  344. VERIFY(is_int32() || is_finite_number());
  345. if (is_int32())
  346. return as_i32();
  347. double value = trunc(as_double());
  348. if (value > INT32_MAX)
  349. return INT32_MAX;
  350. if (value < INT32_MIN)
  351. return INT32_MIN;
  352. return static_cast<i32>(value);
  353. }
  354. bool to_boolean_slow_case() const;
  355. private:
  356. ThrowCompletionOr<Value> to_number_slow_case(VM&) const;
  357. ThrowCompletionOr<Value> to_numeric_slow_case(VM&) const;
  358. ThrowCompletionOr<Value> to_primitive_slow_case(VM&, PreferredType) const;
  359. Value(u64 tag, u64 val)
  360. {
  361. ASSERT(!(tag & val));
  362. m_value.encoded = tag | val;
  363. }
  364. template<typename PointerType>
  365. Value(u64 tag, PointerType const* ptr)
  366. {
  367. if (!ptr) {
  368. // Make sure all nullptrs are null
  369. m_value.tag = NULL_TAG;
  370. return;
  371. }
  372. ASSERT((tag & 0x8000000000000000ul) == 0x8000000000000000ul);
  373. if constexpr (sizeof(PointerType*) < sizeof(u64)) {
  374. m_value.encoded = tag | reinterpret_cast<u32>(ptr);
  375. } else {
  376. // NOTE: Pointers in x86-64 use just 48 bits however are supposed to be
  377. // sign extended up from the 47th bit.
  378. // This means that all bits above the 47th should be the same as
  379. // the 47th. When storing a pointer we thus drop the top 16 bits as
  380. // we can recover it when extracting the pointer again.
  381. // See also: NanBoxedValue::extract_pointer.
  382. m_value.encoded = tag | (reinterpret_cast<u64>(ptr) & 0x0000ffffffffffffULL);
  383. }
  384. }
  385. [[nodiscard]] ThrowCompletionOr<Value> invoke_internal(VM&, PropertyKey const&, Optional<GC::MarkedVector<Value>> arguments);
  386. ThrowCompletionOr<i32> to_i32_slow_case(VM&) const;
  387. friend Value js_undefined();
  388. friend Value js_null();
  389. friend ThrowCompletionOr<Value> greater_than(VM&, Value lhs, Value rhs);
  390. friend ThrowCompletionOr<Value> greater_than_equals(VM&, Value lhs, Value rhs);
  391. friend ThrowCompletionOr<Value> less_than(VM&, Value lhs, Value rhs);
  392. friend ThrowCompletionOr<Value> less_than_equals(VM&, Value lhs, Value rhs);
  393. friend ThrowCompletionOr<Value> add(VM&, Value lhs, Value rhs);
  394. friend bool same_value_non_number(Value lhs, Value rhs);
  395. };
  396. inline Value js_undefined()
  397. {
  398. return Value(UNDEFINED_TAG << GC::TAG_SHIFT, (u64)0);
  399. }
  400. inline Value js_null()
  401. {
  402. return Value(NULL_TAG << GC::TAG_SHIFT, (u64)0);
  403. }
  404. inline Value js_nan()
  405. {
  406. return Value(NAN);
  407. }
  408. inline Value js_infinity()
  409. {
  410. return Value(INFINITY);
  411. }
  412. inline Value js_negative_infinity()
  413. {
  414. return Value(-INFINITY);
  415. }
  416. ThrowCompletionOr<Value> greater_than(VM&, Value lhs, Value rhs);
  417. ThrowCompletionOr<Value> greater_than_equals(VM&, Value lhs, Value rhs);
  418. ThrowCompletionOr<Value> less_than(VM&, Value lhs, Value rhs);
  419. ThrowCompletionOr<Value> less_than_equals(VM&, Value lhs, Value rhs);
  420. ThrowCompletionOr<Value> bitwise_and(VM&, Value lhs, Value rhs);
  421. ThrowCompletionOr<Value> bitwise_or(VM&, Value lhs, Value rhs);
  422. ThrowCompletionOr<Value> bitwise_xor(VM&, Value lhs, Value rhs);
  423. ThrowCompletionOr<Value> bitwise_not(VM&, Value);
  424. ThrowCompletionOr<Value> unary_plus(VM&, Value);
  425. ThrowCompletionOr<Value> unary_minus(VM&, Value);
  426. ThrowCompletionOr<Value> left_shift(VM&, Value lhs, Value rhs);
  427. ThrowCompletionOr<Value> right_shift(VM&, Value lhs, Value rhs);
  428. ThrowCompletionOr<Value> unsigned_right_shift(VM&, Value lhs, Value rhs);
  429. ThrowCompletionOr<Value> add(VM&, Value lhs, Value rhs);
  430. ThrowCompletionOr<Value> sub(VM&, Value lhs, Value rhs);
  431. ThrowCompletionOr<Value> mul(VM&, Value lhs, Value rhs);
  432. ThrowCompletionOr<Value> div(VM&, Value lhs, Value rhs);
  433. ThrowCompletionOr<Value> mod(VM&, Value lhs, Value rhs);
  434. ThrowCompletionOr<Value> exp(VM&, Value lhs, Value rhs);
  435. ThrowCompletionOr<Value> in(VM&, Value lhs, Value rhs);
  436. ThrowCompletionOr<Value> instance_of(VM&, Value lhs, Value rhs);
  437. ThrowCompletionOr<Value> ordinary_has_instance(VM&, Value lhs, Value rhs);
  438. ThrowCompletionOr<bool> is_loosely_equal(VM&, Value lhs, Value rhs);
  439. bool is_strictly_equal(Value lhs, Value rhs);
  440. bool same_value(Value lhs, Value rhs);
  441. bool same_value_zero(Value lhs, Value rhs);
  442. bool same_value_non_number(Value lhs, Value rhs);
  443. ThrowCompletionOr<TriState> is_less_than(VM&, Value lhs, Value rhs, bool left_first);
  444. double to_integer_or_infinity(double);
  445. enum class NumberToStringMode {
  446. WithExponent,
  447. WithoutExponent,
  448. };
  449. [[nodiscard]] String number_to_string(double, NumberToStringMode = NumberToStringMode::WithExponent);
  450. [[nodiscard]] ByteString number_to_byte_string(double, NumberToStringMode = NumberToStringMode::WithExponent);
  451. double string_to_number(StringView);
  452. inline bool Value::operator==(Value const& value) const { return same_value(*this, value); }
  453. }
  454. namespace AK {
  455. static_assert(sizeof(JS::Value) == sizeof(double));
  456. template<>
  457. class Optional<JS::Value> : public OptionalBase<JS::Value> {
  458. template<typename U>
  459. friend class Optional;
  460. public:
  461. using ValueType = JS::Value;
  462. Optional() = default;
  463. template<SameAs<OptionalNone> V>
  464. Optional(V) { }
  465. Optional(Optional<JS::Value> const& other)
  466. {
  467. if (other.has_value())
  468. m_value = other.m_value;
  469. }
  470. Optional(Optional&& other)
  471. : m_value(other.m_value)
  472. {
  473. }
  474. template<typename U = JS::Value>
  475. requires(!IsSame<OptionalNone, RemoveCVReference<U>>)
  476. explicit(!IsConvertible<U&&, JS::Value>) Optional(U&& value)
  477. requires(!IsSame<RemoveCVReference<U>, Optional<JS::Value>> && IsConstructible<JS::Value, U &&>)
  478. : m_value(forward<U>(value))
  479. {
  480. }
  481. template<SameAs<OptionalNone> V>
  482. Optional& operator=(V)
  483. {
  484. clear();
  485. return *this;
  486. }
  487. Optional& operator=(Optional const& other)
  488. {
  489. if (this != &other) {
  490. clear();
  491. m_value = other.m_value;
  492. }
  493. return *this;
  494. }
  495. Optional& operator=(Optional&& other)
  496. {
  497. if (this != &other) {
  498. clear();
  499. m_value = other.m_value;
  500. }
  501. return *this;
  502. }
  503. template<typename O>
  504. ALWAYS_INLINE bool operator==(Optional<O> const& other) const
  505. {
  506. return has_value() == other.has_value() && (!has_value() || value() == other.value());
  507. }
  508. template<typename O>
  509. ALWAYS_INLINE bool operator==(O const& other) const
  510. {
  511. return has_value() && value() == other;
  512. }
  513. void clear()
  514. {
  515. m_value = {};
  516. }
  517. [[nodiscard]] bool has_value() const
  518. {
  519. return !m_value.is_empty();
  520. }
  521. [[nodiscard]] JS::Value& value() &
  522. {
  523. VERIFY(has_value());
  524. return m_value;
  525. }
  526. [[nodiscard]] JS::Value const& value() const&
  527. {
  528. VERIFY(has_value());
  529. return m_value;
  530. }
  531. [[nodiscard]] JS::Value value() &&
  532. {
  533. return release_value();
  534. }
  535. [[nodiscard]] JS::Value release_value()
  536. {
  537. VERIFY(has_value());
  538. JS::Value released_value = m_value;
  539. clear();
  540. return released_value;
  541. }
  542. private:
  543. JS::Value m_value;
  544. };
  545. }
  546. namespace GC {
  547. template<>
  548. class Root<JS::Value> {
  549. public:
  550. Root() = default;
  551. static Root create(JS::Value value, SourceLocation location)
  552. {
  553. if (value.is_cell())
  554. return Root(value, &value.as_cell(), location);
  555. return Root(value);
  556. }
  557. auto cell() { return m_handle.cell(); }
  558. auto cell() const { return m_handle.cell(); }
  559. auto value() const { return *m_value; }
  560. bool is_null() const { return m_handle.is_null() && !m_value.has_value(); }
  561. bool operator==(JS::Value const& value) const { return value == m_value; }
  562. bool operator==(Root<JS::Value> const& other) const { return other.m_value == this->m_value; }
  563. private:
  564. explicit Root(JS::Value value)
  565. : m_value(value)
  566. {
  567. }
  568. explicit Root(JS::Value value, Cell* cell, SourceLocation location)
  569. : m_value(value)
  570. , m_handle(Root<Cell>::create(cell, location))
  571. {
  572. }
  573. Optional<JS::Value> m_value;
  574. Root<Cell> m_handle;
  575. };
  576. inline Root<JS::Value> make_root(JS::Value value, SourceLocation location = SourceLocation::current())
  577. {
  578. return Root<JS::Value>::create(value, location);
  579. }
  580. }
  581. namespace AK {
  582. template<>
  583. struct Formatter<JS::Value> : Formatter<StringView> {
  584. ErrorOr<void> format(FormatBuilder& builder, JS::Value value)
  585. {
  586. if (value.is_empty())
  587. return Formatter<StringView>::format(builder, "<empty>"sv);
  588. return Formatter<StringView>::format(builder, value.to_string_without_side_effects());
  589. }
  590. };
  591. template<>
  592. struct Traits<JS::Value> : DefaultTraits<JS::Value> {
  593. static unsigned hash(JS::Value value) { return Traits<u64>::hash(value.encoded()); }
  594. static constexpr bool is_trivial() { return true; }
  595. };
  596. template<>
  597. struct Traits<GC::Root<JS::Value>> : public DefaultTraits<GC::Root<JS::Value>> {
  598. static unsigned hash(GC::Root<JS::Value> const& handle) { return Traits<JS::Value>::hash(handle.value()); }
  599. };
  600. }