Value.h 7.8 KB

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