Value.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. 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. explicit Value(bool value)
  118. : m_type(Type::Boolean)
  119. {
  120. m_value.as_bool = value;
  121. }
  122. explicit Value(double value)
  123. {
  124. bool is_negative_zero = bit_cast<u64>(value) == NEGATIVE_ZERO_BITS;
  125. if (value >= NumericLimits<i32>::min() && value <= NumericLimits<i32>::max() && trunc(value) == value && !is_negative_zero) {
  126. m_type = Type::Int32;
  127. m_value.as_i32 = static_cast<i32>(value);
  128. } else {
  129. m_type = Type::Double;
  130. m_value.as_double = value;
  131. }
  132. }
  133. explicit Value(unsigned long value)
  134. {
  135. if (value > NumericLimits<i32>::max()) {
  136. m_value.as_double = static_cast<double>(value);
  137. m_type = Type::Double;
  138. } else {
  139. m_value.as_i32 = static_cast<i32>(value);
  140. m_type = Type::Int32;
  141. }
  142. }
  143. explicit Value(unsigned value)
  144. {
  145. if (value > NumericLimits<i32>::max()) {
  146. m_value.as_double = static_cast<double>(value);
  147. m_type = Type::Double;
  148. } else {
  149. m_value.as_i32 = static_cast<i32>(value);
  150. m_type = Type::Int32;
  151. }
  152. }
  153. explicit Value(i32 value)
  154. : m_type(Type::Int32)
  155. {
  156. m_value.as_i32 = value;
  157. }
  158. Value(const Object* object)
  159. : m_type(object ? Type::Object : Type::Null)
  160. {
  161. m_value.as_object = const_cast<Object*>(object);
  162. }
  163. Value(const PrimitiveString* string)
  164. : m_type(Type::String)
  165. {
  166. m_value.as_string = const_cast<PrimitiveString*>(string);
  167. }
  168. Value(const Symbol* symbol)
  169. : m_type(Type::Symbol)
  170. {
  171. m_value.as_symbol = const_cast<Symbol*>(symbol);
  172. }
  173. Value(const Accessor* accessor)
  174. : m_type(Type::Accessor)
  175. {
  176. m_value.as_accessor = const_cast<Accessor*>(accessor);
  177. }
  178. Value(const BigInt* bigint)
  179. : m_type(Type::BigInt)
  180. {
  181. m_value.as_bigint = const_cast<BigInt*>(bigint);
  182. }
  183. explicit Value(Type type)
  184. : m_type(type)
  185. {
  186. }
  187. Type type() const { return m_type; }
  188. double as_double() const
  189. {
  190. VERIFY(is_number());
  191. if (m_type == Type::Int32)
  192. return m_value.as_i32;
  193. return m_value.as_double;
  194. }
  195. bool as_bool() const
  196. {
  197. VERIFY(type() == Type::Boolean);
  198. return m_value.as_bool;
  199. }
  200. Object& as_object()
  201. {
  202. VERIFY(type() == Type::Object);
  203. return *m_value.as_object;
  204. }
  205. const Object& as_object() const
  206. {
  207. VERIFY(type() == Type::Object);
  208. return *m_value.as_object;
  209. }
  210. PrimitiveString& as_string()
  211. {
  212. VERIFY(is_string());
  213. return *m_value.as_string;
  214. }
  215. const PrimitiveString& as_string() const
  216. {
  217. VERIFY(is_string());
  218. return *m_value.as_string;
  219. }
  220. Symbol& as_symbol()
  221. {
  222. VERIFY(is_symbol());
  223. return *m_value.as_symbol;
  224. }
  225. const Symbol& as_symbol() const
  226. {
  227. VERIFY(is_symbol());
  228. return *m_value.as_symbol;
  229. }
  230. Cell& as_cell()
  231. {
  232. VERIFY(is_cell());
  233. return *m_value.as_cell;
  234. }
  235. Accessor& as_accessor()
  236. {
  237. VERIFY(is_accessor());
  238. return *m_value.as_accessor;
  239. }
  240. BigInt& as_bigint()
  241. {
  242. VERIFY(is_bigint());
  243. return *m_value.as_bigint;
  244. }
  245. Array& as_array();
  246. FunctionObject& as_function();
  247. FunctionObject const& as_function() const;
  248. // FIXME: These two conversions are wrong for JS, and seem likely to be footguns
  249. i32 as_i32() const
  250. {
  251. if (m_type == Type::Int32)
  252. return m_value.as_i32;
  253. return static_cast<i32>(as_double());
  254. }
  255. u32 as_u32() const
  256. {
  257. if (m_type == Type::Int32 && m_value.as_i32 >= 0)
  258. return m_value.as_i32;
  259. VERIFY(as_double() >= 0);
  260. return (u32)min(as_double(), (double)NumericLimits<u32>::max());
  261. }
  262. u64 encoded() const { return m_value.encoded; }
  263. ThrowCompletionOr<String> to_string(GlobalObject&) const;
  264. ThrowCompletionOr<Utf16String> to_utf16_string(GlobalObject&) const;
  265. ThrowCompletionOr<PrimitiveString*> to_primitive_string(GlobalObject&);
  266. ThrowCompletionOr<Value> to_primitive(GlobalObject&, PreferredType preferred_type = PreferredType::Default) const;
  267. ThrowCompletionOr<Object*> to_object(GlobalObject&) const;
  268. ThrowCompletionOr<Value> to_numeric(GlobalObject&) const;
  269. ThrowCompletionOr<Value> to_number(GlobalObject&) const;
  270. ThrowCompletionOr<BigInt*> to_bigint(GlobalObject&) const;
  271. ThrowCompletionOr<i64> to_bigint_int64(GlobalObject&) const;
  272. ThrowCompletionOr<u64> to_bigint_uint64(GlobalObject&) const;
  273. ThrowCompletionOr<double> to_double(GlobalObject&) const;
  274. ThrowCompletionOr<StringOrSymbol> to_property_key(GlobalObject&) const;
  275. ThrowCompletionOr<i32> to_i32(GlobalObject& global_object) const;
  276. ThrowCompletionOr<u32> to_u32(GlobalObject&) const;
  277. ThrowCompletionOr<i16> to_i16(GlobalObject&) const;
  278. ThrowCompletionOr<u16> to_u16(GlobalObject&) const;
  279. ThrowCompletionOr<i8> to_i8(GlobalObject&) const;
  280. ThrowCompletionOr<u8> to_u8(GlobalObject&) const;
  281. ThrowCompletionOr<u8> to_u8_clamp(GlobalObject&) const;
  282. ThrowCompletionOr<size_t> to_length(GlobalObject&) const;
  283. ThrowCompletionOr<size_t> to_index(GlobalObject&) const;
  284. ThrowCompletionOr<double> to_integer_or_infinity(GlobalObject&) const;
  285. bool to_boolean() const;
  286. ThrowCompletionOr<Value> get(GlobalObject&, PropertyName const&) const;
  287. ThrowCompletionOr<FunctionObject*> get_method(GlobalObject&, PropertyName const&) const;
  288. String to_string_without_side_effects() const;
  289. Value value_or(Value fallback) const
  290. {
  291. if (is_empty())
  292. return fallback;
  293. return *this;
  294. }
  295. String typeof() const;
  296. bool operator==(Value const&) const;
  297. template<typename... Args>
  298. [[nodiscard]] ALWAYS_INLINE ThrowCompletionOr<Value> invoke(GlobalObject& global_object, PropertyName const& property_name, Args... args);
  299. private:
  300. Type m_type { Type::Empty };
  301. [[nodiscard]] ThrowCompletionOr<Value> invoke_internal(GlobalObject& global_object, PropertyName const&, Optional<MarkedValueList> arguments);
  302. ThrowCompletionOr<i32> to_i32_slow_case(GlobalObject&) const;
  303. union {
  304. bool as_bool;
  305. i32 as_i32;
  306. double as_double;
  307. PrimitiveString* as_string;
  308. Symbol* as_symbol;
  309. Object* as_object;
  310. Cell* as_cell;
  311. Accessor* as_accessor;
  312. BigInt* as_bigint;
  313. u64 encoded;
  314. } m_value { .encoded = 0 };
  315. };
  316. inline Value js_undefined()
  317. {
  318. return Value(Value::Type::Undefined);
  319. }
  320. inline Value js_null()
  321. {
  322. return Value(Value::Type::Null);
  323. }
  324. inline Value js_nan()
  325. {
  326. return Value(NAN);
  327. }
  328. inline Value js_infinity()
  329. {
  330. return Value(INFINITY);
  331. }
  332. inline Value js_negative_infinity()
  333. {
  334. return Value(-INFINITY);
  335. }
  336. inline void Cell::Visitor::visit(Value value)
  337. {
  338. if (value.is_cell())
  339. visit_impl(value.as_cell());
  340. }
  341. ThrowCompletionOr<Value> greater_than(GlobalObject&, Value lhs, Value rhs);
  342. ThrowCompletionOr<Value> greater_than_equals(GlobalObject&, Value lhs, Value rhs);
  343. ThrowCompletionOr<Value> less_than(GlobalObject&, Value lhs, Value rhs);
  344. ThrowCompletionOr<Value> less_than_equals(GlobalObject&, Value lhs, Value rhs);
  345. ThrowCompletionOr<Value> bitwise_and(GlobalObject&, Value lhs, Value rhs);
  346. ThrowCompletionOr<Value> bitwise_or(GlobalObject&, Value lhs, Value rhs);
  347. ThrowCompletionOr<Value> bitwise_xor(GlobalObject&, Value lhs, Value rhs);
  348. ThrowCompletionOr<Value> bitwise_not(GlobalObject&, Value);
  349. ThrowCompletionOr<Value> unary_plus(GlobalObject&, Value);
  350. ThrowCompletionOr<Value> unary_minus(GlobalObject&, Value);
  351. ThrowCompletionOr<Value> left_shift(GlobalObject&, Value lhs, Value rhs);
  352. ThrowCompletionOr<Value> right_shift(GlobalObject&, Value lhs, Value rhs);
  353. ThrowCompletionOr<Value> unsigned_right_shift(GlobalObject&, Value lhs, Value rhs);
  354. ThrowCompletionOr<Value> add(GlobalObject&, Value lhs, Value rhs);
  355. ThrowCompletionOr<Value> sub(GlobalObject&, Value lhs, Value rhs);
  356. ThrowCompletionOr<Value> mul(GlobalObject&, Value lhs, Value rhs);
  357. ThrowCompletionOr<Value> div(GlobalObject&, Value lhs, Value rhs);
  358. ThrowCompletionOr<Value> mod(GlobalObject&, Value lhs, Value rhs);
  359. ThrowCompletionOr<Value> exp(GlobalObject&, Value lhs, Value rhs);
  360. ThrowCompletionOr<Value> in(GlobalObject&, Value lhs, Value rhs);
  361. ThrowCompletionOr<Value> instance_of(GlobalObject&, Value lhs, Value rhs);
  362. ThrowCompletionOr<Value> ordinary_has_instance(GlobalObject&, Value lhs, Value rhs);
  363. ThrowCompletionOr<bool> is_loosely_equal(GlobalObject&, Value lhs, Value rhs);
  364. bool is_strictly_equal(Value lhs, Value rhs);
  365. bool same_value(Value lhs, Value rhs);
  366. bool same_value_zero(Value lhs, Value rhs);
  367. bool same_value_non_numeric(Value lhs, Value rhs);
  368. ThrowCompletionOr<TriState> is_less_than(GlobalObject&, bool left_first, Value lhs, Value rhs);
  369. inline bool Value::operator==(Value const& value) const { return same_value(*this, value); }
  370. struct ValueTraits : public Traits<Value> {
  371. static unsigned hash(Value value)
  372. {
  373. VERIFY(!value.is_empty());
  374. if (value.is_string())
  375. return value.as_string().string().hash();
  376. if (value.is_bigint())
  377. return value.as_bigint().big_integer().hash();
  378. if (value.is_negative_zero())
  379. value = Value(0);
  380. return u64_hash(value.encoded()); // FIXME: Is this the best way to hash pointers, doubles & ints?
  381. }
  382. static bool equals(const Value a, const Value b)
  383. {
  384. return same_value_zero(a, b);
  385. }
  386. };
  387. }
  388. namespace AK {
  389. template<>
  390. struct Formatter<JS::Value> : Formatter<StringView> {
  391. void format(FormatBuilder& builder, const JS::Value& value)
  392. {
  393. Formatter<StringView>::format(builder, value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
  394. }
  395. };
  396. }