Value.h 9.9 KB

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