Value.h 6.8 KB

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