Value.h 8.8 KB

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