Value.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2020, 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/Format.h>
  29. #include <AK/Forward.h>
  30. #include <AK/String.h>
  31. #include <AK/Types.h>
  32. #include <LibJS/Forward.h>
  33. #include <math.h>
  34. // 2 ** 53 - 1
  35. static constexpr double MAX_ARRAY_LIKE_INDEX = 9007199254740991.0;
  36. // 2 ** 32 - 1
  37. static constexpr double MAX_U32 = 4294967295.0;
  38. namespace JS {
  39. class Value {
  40. public:
  41. enum class Type {
  42. Empty,
  43. Undefined,
  44. Null,
  45. Number,
  46. String,
  47. Object,
  48. Boolean,
  49. Symbol,
  50. Accessor,
  51. BigInt,
  52. NativeProperty,
  53. };
  54. enum class PreferredType {
  55. Default,
  56. String,
  57. Number,
  58. };
  59. bool is_empty() const { return m_type == Type::Empty; }
  60. bool is_undefined() const { return m_type == Type::Undefined; }
  61. bool is_null() const { return m_type == Type::Null; }
  62. bool is_number() const { return m_type == Type::Number; }
  63. bool is_string() const { return m_type == Type::String; }
  64. bool is_object() const { return m_type == Type::Object; }
  65. bool is_boolean() const { return m_type == Type::Boolean; }
  66. bool is_symbol() const { return m_type == Type::Symbol; }
  67. bool is_accessor() const { return m_type == Type::Accessor; };
  68. bool is_bigint() const { return m_type == Type::BigInt; };
  69. bool is_native_property() const { return m_type == Type::NativeProperty; }
  70. bool is_nullish() const { return is_null() || is_undefined(); }
  71. bool is_cell() const { return is_string() || is_accessor() || is_object() || is_bigint() || is_symbol() || is_native_property(); }
  72. bool is_array() const;
  73. bool is_function() const;
  74. bool is_regexp(GlobalObject& global_object) const;
  75. bool is_nan() const { return is_number() && __builtin_isnan(as_double()); }
  76. bool is_infinity() const { return is_number() && __builtin_isinf(as_double()); }
  77. bool is_positive_infinity() const { return is_number() && __builtin_isinf_sign(as_double()) > 0; }
  78. bool is_negative_infinity() const { return is_number() && __builtin_isinf_sign(as_double()) < 0; }
  79. bool is_positive_zero() const { return is_number() && 1.0 / as_double() == INFINITY; }
  80. bool is_negative_zero() const { return is_number() && 1.0 / as_double() == -INFINITY; }
  81. bool is_integer() const { return is_finite_number() && (i32)as_double() == as_double(); }
  82. bool is_finite_number() const
  83. {
  84. if (!is_number())
  85. return false;
  86. auto number = as_double();
  87. return !__builtin_isnan(number) && !__builtin_isinf(number);
  88. }
  89. Value()
  90. : m_type(Type::Empty)
  91. {
  92. }
  93. explicit Value(bool value)
  94. : m_type(Type::Boolean)
  95. {
  96. m_value.as_bool = value;
  97. }
  98. explicit Value(double value)
  99. : m_type(Type::Number)
  100. {
  101. m_value.as_double = value;
  102. }
  103. explicit Value(unsigned value)
  104. : m_type(Type::Number)
  105. {
  106. m_value.as_double = static_cast<double>(value);
  107. }
  108. explicit Value(i32 value)
  109. : m_type(Type::Number)
  110. {
  111. m_value.as_double = value;
  112. }
  113. Value(const Object* object)
  114. : m_type(object ? Type::Object : Type::Null)
  115. {
  116. m_value.as_object = const_cast<Object*>(object);
  117. }
  118. Value(const PrimitiveString* string)
  119. : m_type(Type::String)
  120. {
  121. m_value.as_string = const_cast<PrimitiveString*>(string);
  122. }
  123. Value(const Symbol* symbol)
  124. : m_type(Type::Symbol)
  125. {
  126. m_value.as_symbol = const_cast<Symbol*>(symbol);
  127. }
  128. Value(const Accessor* accessor)
  129. : m_type(Type::Accessor)
  130. {
  131. m_value.as_accessor = const_cast<Accessor*>(accessor);
  132. }
  133. Value(const BigInt* bigint)
  134. : m_type(Type::BigInt)
  135. {
  136. m_value.as_bigint = const_cast<BigInt*>(bigint);
  137. }
  138. Value(const NativeProperty* native_property)
  139. : m_type(Type::NativeProperty)
  140. {
  141. m_value.as_native_property = const_cast<NativeProperty*>(native_property);
  142. }
  143. explicit Value(Type type)
  144. : m_type(type)
  145. {
  146. }
  147. Type type() const { return m_type; }
  148. double as_double() const
  149. {
  150. VERIFY(type() == Type::Number);
  151. return m_value.as_double;
  152. }
  153. bool as_bool() const
  154. {
  155. VERIFY(type() == Type::Boolean);
  156. return m_value.as_bool;
  157. }
  158. Object& as_object()
  159. {
  160. VERIFY(type() == Type::Object);
  161. return *m_value.as_object;
  162. }
  163. const Object& as_object() const
  164. {
  165. VERIFY(type() == Type::Object);
  166. return *m_value.as_object;
  167. }
  168. PrimitiveString& as_string()
  169. {
  170. VERIFY(is_string());
  171. return *m_value.as_string;
  172. }
  173. const PrimitiveString& as_string() const
  174. {
  175. VERIFY(is_string());
  176. return *m_value.as_string;
  177. }
  178. Symbol& as_symbol()
  179. {
  180. VERIFY(is_symbol());
  181. return *m_value.as_symbol;
  182. }
  183. const Symbol& as_symbol() const
  184. {
  185. VERIFY(is_symbol());
  186. return *m_value.as_symbol;
  187. }
  188. Cell* as_cell()
  189. {
  190. VERIFY(is_cell());
  191. return m_value.as_cell;
  192. }
  193. Accessor& as_accessor()
  194. {
  195. VERIFY(is_accessor());
  196. return *m_value.as_accessor;
  197. }
  198. BigInt& as_bigint()
  199. {
  200. VERIFY(is_bigint());
  201. return *m_value.as_bigint;
  202. }
  203. NativeProperty& as_native_property()
  204. {
  205. VERIFY(is_native_property());
  206. return *m_value.as_native_property;
  207. }
  208. Array& as_array();
  209. Function& as_function();
  210. i32 as_i32() const;
  211. u32 as_u32() const;
  212. size_t as_size_t() const;
  213. String to_string(GlobalObject&, bool legacy_null_to_empty_string = false) const;
  214. PrimitiveString* to_primitive_string(GlobalObject&);
  215. Value to_primitive(PreferredType preferred_type = PreferredType::Default) const;
  216. Object* to_object(GlobalObject&) const;
  217. Value to_numeric(GlobalObject&) const;
  218. Value to_number(GlobalObject&) const;
  219. BigInt* to_bigint(GlobalObject&) const;
  220. double to_double(GlobalObject&) const;
  221. i32 to_i32(GlobalObject&) const;
  222. u32 to_u32(GlobalObject&) const;
  223. size_t to_length(GlobalObject&) const;
  224. size_t to_index(GlobalObject&) const;
  225. double to_integer_or_infinity(GlobalObject&) const;
  226. bool to_boolean() const;
  227. String to_string_without_side_effects() const;
  228. Value value_or(Value fallback) const
  229. {
  230. if (is_empty())
  231. return fallback;
  232. return *this;
  233. }
  234. private:
  235. Type m_type { Type::Empty };
  236. union {
  237. bool as_bool;
  238. double as_double;
  239. PrimitiveString* as_string;
  240. Symbol* as_symbol;
  241. Object* as_object;
  242. Cell* as_cell;
  243. Accessor* as_accessor;
  244. BigInt* as_bigint;
  245. NativeProperty* as_native_property;
  246. } m_value;
  247. };
  248. inline Value js_undefined()
  249. {
  250. return Value(Value::Type::Undefined);
  251. }
  252. inline Value js_null()
  253. {
  254. return Value(Value::Type::Null);
  255. }
  256. inline Value js_nan()
  257. {
  258. return Value(NAN);
  259. }
  260. inline Value js_infinity()
  261. {
  262. return Value(INFINITY);
  263. }
  264. inline Value js_negative_infinity()
  265. {
  266. return Value(-INFINITY);
  267. }
  268. Value greater_than(GlobalObject&, Value lhs, Value rhs);
  269. Value greater_than_equals(GlobalObject&, Value lhs, Value rhs);
  270. Value less_than(GlobalObject&, Value lhs, Value rhs);
  271. Value less_than_equals(GlobalObject&, Value lhs, Value rhs);
  272. Value bitwise_and(GlobalObject&, Value lhs, Value rhs);
  273. Value bitwise_or(GlobalObject&, Value lhs, Value rhs);
  274. Value bitwise_xor(GlobalObject&, Value lhs, Value rhs);
  275. Value bitwise_not(GlobalObject&, Value);
  276. Value unary_plus(GlobalObject&, Value);
  277. Value unary_minus(GlobalObject&, Value);
  278. Value left_shift(GlobalObject&, Value lhs, Value rhs);
  279. Value right_shift(GlobalObject&, Value lhs, Value rhs);
  280. Value unsigned_right_shift(GlobalObject&, Value lhs, Value rhs);
  281. Value add(GlobalObject&, Value lhs, Value rhs);
  282. Value sub(GlobalObject&, Value lhs, Value rhs);
  283. Value mul(GlobalObject&, Value lhs, Value rhs);
  284. Value div(GlobalObject&, Value lhs, Value rhs);
  285. Value mod(GlobalObject&, Value lhs, Value rhs);
  286. Value exp(GlobalObject&, Value lhs, Value rhs);
  287. Value in(GlobalObject&, Value lhs, Value rhs);
  288. Value instance_of(GlobalObject&, Value lhs, Value rhs);
  289. Value ordinary_has_instance(GlobalObject&, Value lhs, Value rhs);
  290. bool abstract_eq(GlobalObject&, Value lhs, Value rhs);
  291. bool strict_eq(Value lhs, Value rhs);
  292. bool same_value(Value lhs, Value rhs);
  293. bool same_value_zero(Value lhs, Value rhs);
  294. bool same_value_non_numeric(Value lhs, Value rhs);
  295. TriState abstract_relation(GlobalObject&, bool left_first, Value lhs, Value rhs);
  296. Function* get_method(GlobalObject& global_object, Value, const PropertyName&);
  297. size_t length_of_array_like(GlobalObject&, const Object&);
  298. }
  299. namespace AK {
  300. template<>
  301. struct Formatter<JS::Value> : Formatter<StringView> {
  302. void format(FormatBuilder& builder, const JS::Value& value)
  303. {
  304. Formatter<StringView>::format(builder, value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
  305. }
  306. };
  307. }