Object.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 = {}, AllowSideEffects = AllowSideEffects::Yes) 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, AllowSideEffects = AllowSideEffects::Yes) 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(PropertyName const&, bool force_throw_exception = false);
  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_environment_record() const { return false; }
  88. virtual bool is_global_environment_record() const { return false; }
  89. virtual bool is_declarative_environment_record() const { return false; }
  90. virtual bool is_function_environment_record() const { return false; }
  91. // B.3.7 The [[IsHTMLDDA]] Internal Slot, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot
  92. virtual bool is_htmldda() const { return false; }
  93. virtual const char* class_name() const override { return "Object"; }
  94. virtual void visit_edges(Cell::Visitor&) override;
  95. virtual Object* prototype();
  96. virtual const Object* prototype() const;
  97. virtual bool set_prototype(Object* prototype);
  98. bool has_prototype(const Object* prototype) const;
  99. virtual bool is_extensible() const { return m_is_extensible; }
  100. virtual bool prevent_extensions();
  101. bool set_integrity_level(IntegrityLevel);
  102. bool test_integrity_level(IntegrityLevel);
  103. virtual Value value_of() const { return Value(const_cast<Object*>(this)); }
  104. virtual Value ordinary_to_primitive(Value::PreferredType preferred_type) const;
  105. Value get_direct(size_t index) const { return m_storage[index]; }
  106. const IndexedProperties& indexed_properties() const { return m_indexed_properties; }
  107. IndexedProperties& indexed_properties() { return m_indexed_properties; }
  108. void set_indexed_property_elements(Vector<Value>&& values) { m_indexed_properties = IndexedProperties(move(values)); }
  109. void install_error_cause(Value options);
  110. [[nodiscard]] Value invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments);
  111. template<typename... Args>
  112. [[nodiscard]] ALWAYS_INLINE Value invoke(const StringOrSymbol& property_name, Args... args)
  113. {
  114. if constexpr (sizeof...(Args) > 0) {
  115. MarkedValueList arglist { heap() };
  116. (..., arglist.append(move(args)));
  117. return invoke(property_name, move(arglist));
  118. }
  119. return invoke(property_name);
  120. }
  121. void ensure_shape_is_unique();
  122. void enable_transitions() { m_transitions_enabled = true; }
  123. void disable_transitions() { m_transitions_enabled = false; }
  124. template<typename T>
  125. bool fast_is() const = delete;
  126. protected:
  127. enum class GlobalObjectTag { Tag };
  128. enum class ConstructWithoutPrototypeTag { Tag };
  129. explicit Object(GlobalObjectTag);
  130. Object(ConstructWithoutPrototypeTag, GlobalObject&);
  131. virtual Value get_by_index(u32 property_index, AllowSideEffects = AllowSideEffects::Yes) const;
  132. virtual bool put_by_index(u32 property_index, Value);
  133. private:
  134. bool put_own_property(const StringOrSymbol& property_name, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
  135. bool put_own_property_by_index(u32 property_index, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
  136. Value call_native_property_getter(NativeProperty& property, Value this_value) const;
  137. void call_native_property_setter(NativeProperty& property, Value this_value, Value) const;
  138. void set_shape(Shape&);
  139. bool m_is_extensible { true };
  140. bool m_transitions_enabled { true };
  141. Shape* m_shape { nullptr };
  142. Vector<Value> m_storage;
  143. IndexedProperties m_indexed_properties;
  144. };
  145. template<>
  146. [[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name, MarkedValueList arguments) { return invoke_internal(property_name, move(arguments)); }
  147. template<>
  148. [[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments) { return invoke_internal(property_name, move(arguments)); }
  149. template<>
  150. [[nodiscard]] ALWAYS_INLINE Value Object::invoke(const StringOrSymbol& property_name) { return invoke(property_name, Optional<MarkedValueList> {}); }
  151. }