Value.h 5.1 KB

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