Value.h 9.3 KB

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