Value.h 9.3 KB

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