Object.h 8.1 KB

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