Value.h 5.4 KB

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