Object.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <AK/String.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibJS/Heap/Cell.h>
  12. #include <LibJS/Runtime/IndexedProperties.h>
  13. #include <LibJS/Runtime/MarkedValueList.h>
  14. #include <LibJS/Runtime/PrimitiveString.h>
  15. #include <LibJS/Runtime/PropertyName.h>
  16. #include <LibJS/Runtime/Shape.h>
  17. #include <LibJS/Runtime/Value.h>
  18. namespace JS {
  19. #define JS_OBJECT(class_, base_class) \
  20. public: \
  21. using Base = base_class; \
  22. virtual const char* class_name() const override { return #class_; }
  23. struct PropertyDescriptor {
  24. PropertyAttributes attributes;
  25. Value value;
  26. Function* getter { nullptr };
  27. Function* setter { nullptr };
  28. static PropertyDescriptor from_dictionary(VM&, const Object&);
  29. bool is_accessor_descriptor() const { return getter || setter; }
  30. bool is_data_descriptor() const { return !(value.is_empty() && !attributes.has_writable()); }
  31. bool is_generic_descriptor() const { return !is_accessor_descriptor() && !is_data_descriptor(); }
  32. };
  33. class Object : public Cell {
  34. public:
  35. static Object* create(GlobalObject&, Object* prototype);
  36. explicit Object(Object& prototype);
  37. explicit Object(Shape&);
  38. virtual void initialize(GlobalObject&) override;
  39. virtual ~Object();
  40. enum class PropertyKind {
  41. Key,
  42. Value,
  43. KeyAndValue,
  44. };
  45. enum class GetOwnPropertyReturnType {
  46. All,
  47. StringOnly,
  48. SymbolOnly,
  49. };
  50. enum class PutOwnPropertyMode {
  51. Put,
  52. DefineProperty,
  53. };
  54. enum class IntegrityLevel {
  55. Sealed,
  56. Frozen,
  57. };
  58. Shape& shape() { return *m_shape; }
  59. const Shape& shape() const { return *m_shape; }
  60. GlobalObject& global_object() const { return *shape().global_object(); }
  61. virtual Value get(const PropertyName&, Value receiver = {}, bool without_side_effects = false) const;
  62. Value get_without_side_effects(const PropertyName&) const;
  63. virtual bool has_property(const PropertyName&) const;
  64. bool has_own_property(const PropertyName&) const;
  65. virtual bool put(const PropertyName&, Value, Value receiver = {});
  66. Value get_own_property(const PropertyName&, Value receiver, bool without_side_effects = false) const;
  67. MarkedValueList get_own_properties(PropertyKind, bool only_enumerable_properties = false, GetOwnPropertyReturnType = GetOwnPropertyReturnType::All) const;
  68. MarkedValueList get_enumerable_own_property_names(PropertyKind) const;
  69. virtual Optional<PropertyDescriptor> get_own_property_descriptor(const PropertyName&) const;
  70. Value get_own_property_descriptor_object(const PropertyName&) const;
  71. virtual bool define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions = true);
  72. bool define_property(const PropertyName&, Value value, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
  73. bool define_property_without_transition(const PropertyName&, Value value, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
  74. bool define_accessor(const PropertyName&, Function* getter, Function* setter, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
  75. bool define_native_function(PropertyName const&, AK::Function<Value(VM&, GlobalObject&)>, i32 length = 0, PropertyAttributes attributes = default_attributes);
  76. bool define_native_property(PropertyName const&, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attributes = default_attributes);
  77. bool define_native_accessor(PropertyName const&, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attributes = default_attributes);
  78. void define_properties(Value properties);
  79. virtual bool delete_property(const PropertyName&);
  80. virtual bool is_array() const { return false; }
  81. virtual bool is_function() const { return false; }
  82. virtual bool is_typed_array() const { return false; }
  83. virtual bool is_string_object() const { return false; }
  84. virtual bool is_global_object() const { return false; }
  85. virtual bool is_proxy_object() const { return false; }
  86. virtual bool is_native_function() const { return false; }
  87. virtual bool is_lexical_environment() const { return false; }
  88. virtual const char* class_name() const override { return "Object"; }
  89. virtual void visit_edges(Cell::Visitor&) override;
  90. virtual Object* prototype();
  91. virtual const Object* prototype() const;
  92. virtual bool set_prototype(Object* prototype);
  93. bool has_prototype(const Object* prototype) const;
  94. virtual bool is_extensible() const { return m_is_extensible; }
  95. virtual bool prevent_extensions();
  96. bool set_integrity_level(IntegrityLevel);
  97. bool test_integrity_level(IntegrityLevel);
  98. virtual Value value_of() const { return Value(const_cast<Object*>(this)); }
  99. virtual Value ordinary_to_primitive(Value::PreferredType preferred_type) const;
  100. Value get_direct(size_t index) const { return m_storage[index]; }
  101. const IndexedProperties& indexed_properties() const { return m_indexed_properties; }
  102. IndexedProperties& indexed_properties() { return m_indexed_properties; }
  103. void set_indexed_property_elements(Vector<Value>&& values) { m_indexed_properties = IndexedProperties(move(values)); }
  104. void install_error_cause(Value options);
  105. [[nodiscard]] Value invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments);
  106. template<typename... Args>
  107. [[nodiscard]] ALWAYS_INLINE Value invoke(const StringOrSymbol& property_name, Args... args)
  108. {
  109. if constexpr (sizeof...(Args) > 0) {
  110. MarkedValueList arglist { heap() };
  111. (..., arglist.append(move(args)));
  112. return invoke(property_name, move(arglist));
  113. }
  114. return invoke(property_name);
  115. }
  116. void ensure_shape_is_unique();
  117. void enable_transitions() { m_transitions_enabled = true; }
  118. void disable_transitions() { m_transitions_enabled = false; }
  119. template<typename T>
  120. bool fast_is() const = delete;
  121. protected:
  122. enum class GlobalObjectTag { Tag };
  123. enum class ConstructWithoutPrototypeTag { Tag };
  124. explicit Object(GlobalObjectTag);
  125. Object(ConstructWithoutPrototypeTag, GlobalObject&);
  126. virtual Value get_by_index(u32 property_index, bool without_side_effects = false) const;
  127. virtual bool put_by_index(u32 property_index, Value);
  128. private:
  129. bool put_own_property(const StringOrSymbol& property_name, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
  130. bool put_own_property_by_index(u32 property_index, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
  131. Value call_native_property_getter(NativeProperty& property, Value this_value) const;
  132. void call_native_property_setter(NativeProperty& property, Value this_value, Value) const;
  133. void set_shape(Shape&);
  134. bool m_is_extensible { true };
  135. bool m_transitions_enabled { true };
  136. Shape* m_shape { nullptr };
  137. Vector<Value> m_storage;
  138. IndexedProperties m_indexed_properties;
  139. };
  140. template<>
  141. [[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name, MarkedValueList arguments) { return invoke_internal(property_name, move(arguments)); }
  142. template<>
  143. [[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments) { return invoke_internal(property_name, move(arguments)); }
  144. template<>
  145. [[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name) { return invoke(property_name, Optional<MarkedValueList> {}); }
  146. }