Object.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <AK/HashMap.h>
  29. #include <AK/String.h>
  30. #include <LibJS/Forward.h>
  31. #include <LibJS/Runtime/Cell.h>
  32. #include <LibJS/Runtime/IndexedProperties.h>
  33. #include <LibJS/Runtime/MarkedValueList.h>
  34. #include <LibJS/Runtime/PrimitiveString.h>
  35. #include <LibJS/Runtime/PropertyName.h>
  36. #include <LibJS/Runtime/Shape.h>
  37. #include <LibJS/Runtime/Value.h>
  38. namespace JS {
  39. #define JS_OBJECT(class_, base_class) \
  40. public: \
  41. using Base = base_class; \
  42. virtual const char* class_name() const override { return #class_; }
  43. struct PropertyDescriptor {
  44. PropertyAttributes attributes;
  45. Value value;
  46. Function* getter { nullptr };
  47. Function* setter { nullptr };
  48. static PropertyDescriptor from_dictionary(VM&, const Object&);
  49. bool is_accessor_descriptor() const { return getter || setter; }
  50. bool is_data_descriptor() const { return !(value.is_empty() && !attributes.has_writable()); }
  51. bool is_generic_descriptor() const { return !is_accessor_descriptor() && !is_data_descriptor(); }
  52. };
  53. class Object : public Cell {
  54. public:
  55. static Object* create_empty(GlobalObject&);
  56. explicit Object(Object& prototype);
  57. explicit Object(Shape&);
  58. virtual void initialize(GlobalObject&) override;
  59. virtual ~Object();
  60. enum class PropertyKind {
  61. Key,
  62. Value,
  63. KeyAndValue,
  64. };
  65. enum class GetOwnPropertyReturnType {
  66. All,
  67. StringOnly,
  68. SymbolOnly,
  69. };
  70. enum class PutOwnPropertyMode {
  71. Put,
  72. DefineProperty,
  73. };
  74. enum class IntegrityLevel {
  75. Sealed,
  76. Frozen,
  77. };
  78. Shape& shape() { return *m_shape; }
  79. const Shape& shape() const { return *m_shape; }
  80. GlobalObject& global_object() const { return *shape().global_object(); }
  81. virtual Value get(const PropertyName&, Value receiver = {}, bool without_side_effects = false) const;
  82. Value get_without_side_effects(const PropertyName&) const;
  83. virtual bool has_property(const PropertyName&) const;
  84. bool has_own_property(const PropertyName&) const;
  85. virtual bool put(const PropertyName&, Value, Value receiver = {});
  86. Value get_own_property(const PropertyName&, Value receiver, bool without_side_effects = false) const;
  87. MarkedValueList get_own_properties(PropertyKind, bool only_enumerable_properties = false, GetOwnPropertyReturnType = GetOwnPropertyReturnType::All) const;
  88. MarkedValueList get_enumerable_own_property_names(PropertyKind) const;
  89. virtual Optional<PropertyDescriptor> get_own_property_descriptor(const PropertyName&) const;
  90. Value get_own_property_descriptor_object(const PropertyName&) const;
  91. virtual bool define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions = true);
  92. bool define_property(const PropertyName&, Value value, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
  93. bool define_property_without_transition(const PropertyName&, Value value, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
  94. bool define_accessor(const PropertyName&, Function* getter, Function* setter, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
  95. bool define_native_function(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)>, i32 length = 0, PropertyAttributes attributes = default_attributes);
  96. bool define_native_property(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attributes = default_attributes);
  97. void define_properties(Value properties);
  98. virtual bool delete_property(const PropertyName&);
  99. virtual bool is_array() const { return false; }
  100. virtual bool is_function() const { return false; }
  101. virtual bool is_typed_array() const { return false; }
  102. virtual bool is_string_object() const { return false; }
  103. virtual bool is_global_object() const { return false; }
  104. virtual const char* class_name() const override { return "Object"; }
  105. virtual void visit_edges(Cell::Visitor&) override;
  106. virtual Object* prototype();
  107. virtual const Object* prototype() const;
  108. virtual bool set_prototype(Object* prototype);
  109. bool has_prototype(const Object* prototype) const;
  110. virtual bool is_extensible() const { return m_is_extensible; }
  111. virtual bool prevent_extensions();
  112. bool set_integrity_level(IntegrityLevel);
  113. bool test_integrity_level(IntegrityLevel);
  114. virtual Value value_of() const { return Value(const_cast<Object*>(this)); }
  115. virtual Value ordinary_to_primitive(Value::PreferredType preferred_type) const;
  116. Value get_direct(size_t index) const { return m_storage[index]; }
  117. const IndexedProperties& indexed_properties() const { return m_indexed_properties; }
  118. IndexedProperties& indexed_properties() { return m_indexed_properties; }
  119. void set_indexed_property_elements(Vector<Value>&& values) { m_indexed_properties = IndexedProperties(move(values)); }
  120. [[nodiscard]] Value invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments);
  121. template<typename... Args>
  122. [[nodiscard]] ALWAYS_INLINE Value invoke(const StringOrSymbol& property_name, Args... args)
  123. {
  124. if constexpr (sizeof...(Args) > 0) {
  125. MarkedValueList arglist { heap() };
  126. (..., arglist.append(move(args)));
  127. return invoke(property_name, move(arglist));
  128. }
  129. return invoke(property_name);
  130. }
  131. void ensure_shape_is_unique();
  132. void enable_transitions() { m_transitions_enabled = true; }
  133. void disable_transitions() { m_transitions_enabled = false; }
  134. template<typename T>
  135. bool fast_is() const = delete;
  136. protected:
  137. enum class GlobalObjectTag { Tag };
  138. enum class ConstructWithoutPrototypeTag { Tag };
  139. explicit Object(GlobalObjectTag);
  140. Object(ConstructWithoutPrototypeTag, GlobalObject&);
  141. virtual Value get_by_index(u32 property_index) const;
  142. virtual bool put_by_index(u32 property_index, Value);
  143. private:
  144. bool put_own_property(const StringOrSymbol& property_name, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
  145. bool put_own_property_by_index(u32 property_index, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
  146. Value call_native_property_getter(NativeProperty& property, Value this_value) const;
  147. void call_native_property_setter(NativeProperty& property, Value this_value, Value) const;
  148. void set_shape(Shape&);
  149. bool m_is_extensible { true };
  150. bool m_transitions_enabled { true };
  151. Shape* m_shape { nullptr };
  152. Vector<Value> m_storage;
  153. IndexedProperties m_indexed_properties;
  154. };
  155. template<>
  156. [[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name, MarkedValueList arguments) { return invoke_internal(property_name, move(arguments)); }
  157. template<>
  158. [[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments) { return invoke_internal(property_name, move(arguments)); }
  159. template<>
  160. [[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name) { return invoke(property_name, Optional<MarkedValueList> {}); }
  161. }