Value.h 8.3 KB

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