Value.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Assertions.h>
  28. #include <AK/BitCast.h>
  29. #include <AK/Format.h>
  30. #include <AK/Forward.h>
  31. #include <AK/String.h>
  32. #include <AK/Types.h>
  33. #include <LibJS/Forward.h>
  34. #include <math.h>
  35. // 2 ** 53 - 1
  36. static constexpr double MAX_ARRAY_LIKE_INDEX = 9007199254740991.0;
  37. // 2 ** 32 - 1
  38. static constexpr double MAX_U32 = 4294967295.0;
  39. // Unique bit representation of negative zero (only sign bit set)
  40. static constexpr u64 NEGATIVE_ZERO_BITS = ((u64)1 << 63);
  41. namespace JS {
  42. class Value {
  43. public:
  44. enum class Type {
  45. Empty,
  46. Undefined,
  47. Null,
  48. Int32,
  49. Double,
  50. String,
  51. Object,
  52. Boolean,
  53. Symbol,
  54. Accessor,
  55. BigInt,
  56. NativeProperty,
  57. };
  58. enum class PreferredType {
  59. Default,
  60. String,
  61. Number,
  62. };
  63. bool is_empty() const { return m_type == Type::Empty; }
  64. bool is_undefined() const { return m_type == Type::Undefined; }
  65. bool is_null() const { return m_type == Type::Null; }
  66. bool is_number() const { return m_type == Type::Int32 || m_type == Type::Double; }
  67. bool is_string() const { return m_type == Type::String; }
  68. bool is_object() const { return m_type == Type::Object; }
  69. bool is_boolean() const { return m_type == Type::Boolean; }
  70. bool is_symbol() const { return m_type == Type::Symbol; }
  71. bool is_accessor() const { return m_type == Type::Accessor; };
  72. bool is_bigint() const { return m_type == Type::BigInt; };
  73. bool is_native_property() const { return m_type == Type::NativeProperty; }
  74. bool is_nullish() const { return is_null() || is_undefined(); }
  75. bool is_cell() const { return is_string() || is_accessor() || is_object() || is_bigint() || is_symbol() || is_native_property(); }
  76. bool is_array() const;
  77. bool is_function() const;
  78. bool is_regexp(GlobalObject& global_object) const;
  79. bool is_nan() const { return is_number() && __builtin_isnan(as_double()); }
  80. bool is_infinity() const { return is_number() && __builtin_isinf(as_double()); }
  81. bool is_positive_infinity() const { return is_number() && __builtin_isinf_sign(as_double()) > 0; }
  82. bool is_negative_infinity() const { return is_number() && __builtin_isinf_sign(as_double()) < 0; }
  83. bool is_positive_zero() const { return is_number() && bit_cast<u64>(as_double()) == 0; }
  84. bool is_negative_zero() const { return is_number() && bit_cast<u64>(as_double()) == NEGATIVE_ZERO_BITS; }
  85. bool is_integer() const { return is_finite_number() && (i32)as_double() == as_double(); }
  86. bool is_finite_number() const
  87. {
  88. if (!is_number())
  89. return false;
  90. auto number = as_double();
  91. return !__builtin_isnan(number) && !__builtin_isinf(number);
  92. }
  93. Value()
  94. : m_type(Type::Empty)
  95. {
  96. }
  97. explicit Value(bool value)
  98. : m_type(Type::Boolean)
  99. {
  100. m_value.as_bool = value;
  101. }
  102. explicit Value(double value)
  103. {
  104. bool is_negative_zero = bit_cast<u64>(value) == NEGATIVE_ZERO_BITS;
  105. if (value >= NumericLimits<i32>::min() && value <= NumericLimits<i32>::max() && trunc(value) == value && !is_negative_zero) {
  106. m_type = Type::Int32;
  107. m_value.as_i32 = static_cast<i32>(value);
  108. } else {
  109. m_type = Type::Double;
  110. m_value.as_double = value;
  111. }
  112. }
  113. explicit Value(unsigned value)
  114. {
  115. if (value > NumericLimits<i32>::max()) {
  116. m_value.as_double = static_cast<double>(value);
  117. m_type = Type::Double;
  118. } else {
  119. m_value.as_i32 = static_cast<i32>(value);
  120. m_type = Type::Int32;
  121. }
  122. }
  123. explicit Value(i32 value)
  124. : m_type(Type::Int32)
  125. {
  126. m_value.as_i32 = value;
  127. }
  128. Value(const Object* object)
  129. : m_type(object ? Type::Object : Type::Null)
  130. {
  131. m_value.as_object = const_cast<Object*>(object);
  132. }
  133. Value(const PrimitiveString* string)
  134. : m_type(Type::String)
  135. {
  136. m_value.as_string = const_cast<PrimitiveString*>(string);
  137. }
  138. Value(const Symbol* symbol)
  139. : m_type(Type::Symbol)
  140. {
  141. m_value.as_symbol = const_cast<Symbol*>(symbol);
  142. }
  143. Value(const Accessor* accessor)
  144. : m_type(Type::Accessor)
  145. {
  146. m_value.as_accessor = const_cast<Accessor*>(accessor);
  147. }
  148. Value(const BigInt* bigint)
  149. : m_type(Type::BigInt)
  150. {
  151. m_value.as_bigint = const_cast<BigInt*>(bigint);
  152. }
  153. Value(const NativeProperty* native_property)
  154. : m_type(Type::NativeProperty)
  155. {
  156. m_value.as_native_property = const_cast<NativeProperty*>(native_property);
  157. }
  158. explicit Value(Type type)
  159. : m_type(type)
  160. {
  161. }
  162. Type type() const { return m_type; }
  163. double as_double() const
  164. {
  165. VERIFY(is_number());
  166. if (m_type == Type::Int32)
  167. return m_value.as_i32;
  168. return m_value.as_double;
  169. }
  170. bool as_bool() const
  171. {
  172. VERIFY(type() == Type::Boolean);
  173. return m_value.as_bool;
  174. }
  175. Object& as_object()
  176. {
  177. VERIFY(type() == Type::Object);
  178. return *m_value.as_object;
  179. }
  180. const Object& as_object() const
  181. {
  182. VERIFY(type() == Type::Object);
  183. return *m_value.as_object;
  184. }
  185. PrimitiveString& as_string()
  186. {
  187. VERIFY(is_string());
  188. return *m_value.as_string;
  189. }
  190. const PrimitiveString& as_string() const
  191. {
  192. VERIFY(is_string());
  193. return *m_value.as_string;
  194. }
  195. Symbol& as_symbol()
  196. {
  197. VERIFY(is_symbol());
  198. return *m_value.as_symbol;
  199. }
  200. const Symbol& as_symbol() const
  201. {
  202. VERIFY(is_symbol());
  203. return *m_value.as_symbol;
  204. }
  205. Cell* as_cell()
  206. {
  207. VERIFY(is_cell());
  208. return m_value.as_cell;
  209. }
  210. Accessor& as_accessor()
  211. {
  212. VERIFY(is_accessor());
  213. return *m_value.as_accessor;
  214. }
  215. BigInt& as_bigint()
  216. {
  217. VERIFY(is_bigint());
  218. return *m_value.as_bigint;
  219. }
  220. NativeProperty& as_native_property()
  221. {
  222. VERIFY(is_native_property());
  223. return *m_value.as_native_property;
  224. }
  225. Array& as_array();
  226. Function& as_function();
  227. i32 as_i32() const;
  228. u32 as_u32() const;
  229. String to_string(GlobalObject&, bool legacy_null_to_empty_string = false) const;
  230. PrimitiveString* to_primitive_string(GlobalObject&);
  231. Value to_primitive(GlobalObject&, PreferredType preferred_type = PreferredType::Default) const;
  232. Object* to_object(GlobalObject&) const;
  233. Value to_numeric(GlobalObject&) const;
  234. Value to_number(GlobalObject&) const;
  235. BigInt* to_bigint(GlobalObject&) const;
  236. double to_double(GlobalObject&) const;
  237. i32 to_i32(GlobalObject& global_object) const
  238. {
  239. if (m_type == Type::Int32)
  240. return m_value.as_i32;
  241. return to_i32_slow_case(global_object);
  242. }
  243. u32 to_u32(GlobalObject&) const;
  244. size_t to_length(GlobalObject&) const;
  245. size_t to_index(GlobalObject&) const;
  246. double to_integer_or_infinity(GlobalObject&) const;
  247. bool to_boolean() const;
  248. String to_string_without_side_effects() const;
  249. Value value_or(Value fallback) const
  250. {
  251. if (is_empty())
  252. return fallback;
  253. return *this;
  254. }
  255. private:
  256. Type m_type { Type::Empty };
  257. i32 to_i32_slow_case(GlobalObject&) const;
  258. union {
  259. bool as_bool;
  260. i32 as_i32;
  261. double as_double;
  262. PrimitiveString* as_string;
  263. Symbol* as_symbol;
  264. Object* as_object;
  265. Cell* as_cell;
  266. Accessor* as_accessor;
  267. BigInt* as_bigint;
  268. NativeProperty* as_native_property;
  269. } m_value;
  270. };
  271. inline Value js_undefined()
  272. {
  273. return Value(Value::Type::Undefined);
  274. }
  275. inline Value js_null()
  276. {
  277. return Value(Value::Type::Null);
  278. }
  279. inline Value js_nan()
  280. {
  281. return Value(NAN);
  282. }
  283. inline Value js_infinity()
  284. {
  285. return Value(INFINITY);
  286. }
  287. inline Value js_negative_infinity()
  288. {
  289. return Value(-INFINITY);
  290. }
  291. Value greater_than(GlobalObject&, Value lhs, Value rhs);
  292. Value greater_than_equals(GlobalObject&, Value lhs, Value rhs);
  293. Value less_than(GlobalObject&, Value lhs, Value rhs);
  294. Value less_than_equals(GlobalObject&, Value lhs, Value rhs);
  295. Value bitwise_and(GlobalObject&, Value lhs, Value rhs);
  296. Value bitwise_or(GlobalObject&, Value lhs, Value rhs);
  297. Value bitwise_xor(GlobalObject&, Value lhs, Value rhs);
  298. Value bitwise_not(GlobalObject&, Value);
  299. Value unary_plus(GlobalObject&, Value);
  300. Value unary_minus(GlobalObject&, Value);
  301. Value left_shift(GlobalObject&, Value lhs, Value rhs);
  302. Value right_shift(GlobalObject&, Value lhs, Value rhs);
  303. Value unsigned_right_shift(GlobalObject&, Value lhs, Value rhs);
  304. Value add(GlobalObject&, Value lhs, Value rhs);
  305. Value sub(GlobalObject&, Value lhs, Value rhs);
  306. Value mul(GlobalObject&, Value lhs, Value rhs);
  307. Value div(GlobalObject&, Value lhs, Value rhs);
  308. Value mod(GlobalObject&, Value lhs, Value rhs);
  309. Value exp(GlobalObject&, Value lhs, Value rhs);
  310. Value in(GlobalObject&, Value lhs, Value rhs);
  311. Value instance_of(GlobalObject&, Value lhs, Value rhs);
  312. Value ordinary_has_instance(GlobalObject&, Value lhs, Value rhs);
  313. bool abstract_eq(GlobalObject&, Value lhs, Value rhs);
  314. bool strict_eq(Value lhs, Value rhs);
  315. bool same_value(Value lhs, Value rhs);
  316. bool same_value_zero(Value lhs, Value rhs);
  317. bool same_value_non_numeric(Value lhs, Value rhs);
  318. TriState abstract_relation(GlobalObject&, bool left_first, Value lhs, Value rhs);
  319. Function* get_method(GlobalObject& global_object, Value, const PropertyName&);
  320. size_t length_of_array_like(GlobalObject&, const Object&);
  321. }
  322. namespace AK {
  323. template<>
  324. struct Formatter<JS::Value> : Formatter<StringView> {
  325. void format(FormatBuilder& builder, const JS::Value& value)
  326. {
  327. Formatter<StringView>::format(builder, value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
  328. }
  329. };
  330. }