Value.h 8.3 KB

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