Value.h 6.5 KB

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