Value.h 13 KB

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