Value.h 10.0 KB

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