Object.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (c) 2020-2024, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Badge.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/StringView.h>
  11. #include <LibJS/Forward.h>
  12. #include <LibJS/Heap/Cell.h>
  13. #include <LibJS/Heap/CellAllocator.h>
  14. #include <LibJS/Heap/MarkedVector.h>
  15. #include <LibJS/Runtime/Completion.h>
  16. #include <LibJS/Runtime/IndexedProperties.h>
  17. #include <LibJS/Runtime/PrimitiveString.h>
  18. #include <LibJS/Runtime/PrivateEnvironment.h>
  19. #include <LibJS/Runtime/PropertyDescriptor.h>
  20. #include <LibJS/Runtime/PropertyKey.h>
  21. #include <LibJS/Runtime/Shape.h>
  22. #include <LibJS/Runtime/Value.h>
  23. #include <LibJS/SafeFunction.h>
  24. namespace JS {
  25. #define JS_OBJECT(class_, base_class) JS_CELL(class_, base_class)
  26. struct PrivateElement {
  27. enum class Kind {
  28. Field,
  29. Method,
  30. Accessor
  31. };
  32. PrivateName key;
  33. Kind kind { Kind::Field };
  34. Value value;
  35. };
  36. // Non-standard: This is information optionally returned by object property access functions.
  37. // It can be used to implement inline caches for property lookup.
  38. struct CacheablePropertyMetadata {
  39. enum class Type {
  40. NotCacheable,
  41. OwnProperty,
  42. InPrototypeChain,
  43. };
  44. Type type { Type::NotCacheable };
  45. Optional<u32> property_offset;
  46. GCPtr<Object const> prototype;
  47. };
  48. class Object : public Cell {
  49. JS_CELL(Object, Cell);
  50. JS_DECLARE_ALLOCATOR(Object);
  51. public:
  52. static NonnullGCPtr<Object> create_prototype(Realm&, Object* prototype);
  53. static NonnullGCPtr<Object> create(Realm&, Object* prototype);
  54. static NonnullGCPtr<Object> create_with_premade_shape(Shape&);
  55. virtual void initialize(Realm&) override;
  56. virtual ~Object();
  57. enum class PropertyKind {
  58. Key,
  59. Value,
  60. KeyAndValue,
  61. };
  62. enum class IntegrityLevel {
  63. Sealed,
  64. Frozen,
  65. };
  66. enum class ShouldThrowExceptions {
  67. No,
  68. Yes,
  69. };
  70. enum class MayInterfereWithIndexedPropertyAccess {
  71. No,
  72. Yes,
  73. };
  74. // Please DO NOT make up your own non-standard methods unless you
  75. // have a very good reason to do so. If any object abstract
  76. // operation from the spec is missing, add it instead.
  77. // Functionality for implementation details like shapes and
  78. // property storage are obviously exempt from this rule :^)
  79. //
  80. // Methods named [[Foo]]() in the spec are named internal_foo()
  81. // here, as they are "The [[Foo]] internal method of a ... object".
  82. // They must be virtual and may be overridden. All other methods
  83. // follow the regular PascalCase name converted to camel_case
  84. // naming convention and must not be virtual.
  85. // 7.1 Type Conversion, https://tc39.es/ecma262/#sec-type-conversion
  86. ThrowCompletionOr<Value> ordinary_to_primitive(Value::PreferredType preferred_type) const;
  87. // 7.2 Testing and Comparison Operations, https://tc39.es/ecma262/#sec-testing-and-comparison-operations
  88. ThrowCompletionOr<bool> is_extensible() const;
  89. // 7.3 Operations on Objects, https://tc39.es/ecma262/#sec-operations-on-objects
  90. ThrowCompletionOr<Value> get(PropertyKey const&) const;
  91. ThrowCompletionOr<void> set(PropertyKey const&, Value, ShouldThrowExceptions);
  92. ThrowCompletionOr<bool> create_data_property(PropertyKey const&, Value);
  93. void create_method_property(PropertyKey const&, Value);
  94. ThrowCompletionOr<bool> create_data_property_or_throw(PropertyKey const&, Value);
  95. void create_non_enumerable_data_property_or_throw(PropertyKey const&, Value);
  96. ThrowCompletionOr<void> define_property_or_throw(PropertyKey const&, PropertyDescriptor const&);
  97. ThrowCompletionOr<void> delete_property_or_throw(PropertyKey const&);
  98. ThrowCompletionOr<bool> has_property(PropertyKey const&) const;
  99. ThrowCompletionOr<bool> has_own_property(PropertyKey const&) const;
  100. ThrowCompletionOr<bool> set_integrity_level(IntegrityLevel);
  101. ThrowCompletionOr<bool> test_integrity_level(IntegrityLevel) const;
  102. ThrowCompletionOr<MarkedVector<Value>> enumerable_own_property_names(PropertyKind kind) const;
  103. ThrowCompletionOr<void> copy_data_properties(VM&, Value source, HashTable<PropertyKey> const& excluded_keys, HashTable<JS::Value> const& excluded_values = {});
  104. ThrowCompletionOr<NonnullGCPtr<Object>> snapshot_own_properties(VM&, GCPtr<Object> prototype, HashTable<PropertyKey> const& excluded_keys = {}, HashTable<Value> const& excluded_values = {});
  105. PrivateElement* private_element_find(PrivateName const& name);
  106. ThrowCompletionOr<void> private_field_add(PrivateName const& name, Value value);
  107. ThrowCompletionOr<void> private_method_or_accessor_add(PrivateElement element);
  108. ThrowCompletionOr<Value> private_get(PrivateName const& name);
  109. ThrowCompletionOr<void> private_set(PrivateName const& name, Value value);
  110. ThrowCompletionOr<void> define_field(ClassFieldDefinition const&);
  111. ThrowCompletionOr<void> initialize_instance_elements(ECMAScriptFunctionObject& constructor);
  112. // 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
  113. virtual ThrowCompletionOr<Object*> internal_get_prototype_of() const;
  114. virtual ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype);
  115. virtual ThrowCompletionOr<bool> internal_is_extensible() const;
  116. virtual ThrowCompletionOr<bool> internal_prevent_extensions();
  117. virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const;
  118. virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&);
  119. virtual ThrowCompletionOr<bool> internal_has_property(PropertyKey const&) const;
  120. enum class PropertyLookupPhase {
  121. OwnProperty,
  122. PrototypeChain,
  123. };
  124. virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver, CacheablePropertyMetadata* = nullptr, PropertyLookupPhase = PropertyLookupPhase::OwnProperty) const;
  125. virtual ThrowCompletionOr<bool> internal_set(PropertyKey const&, Value value, Value receiver, CacheablePropertyMetadata* = nullptr);
  126. virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&);
  127. virtual ThrowCompletionOr<MarkedVector<Value>> internal_own_property_keys() const;
  128. // NOTE: Any subclass of Object that overrides property access slots ([[Get]], [[Set]] etc)
  129. // to customize access to indexed properties (properties where the name is a positive integer)
  130. // must return true for this, to opt out of optimizations that rely on assumptions that
  131. // might not hold when property access behaves differently.
  132. bool may_interfere_with_indexed_property_access() const { return m_may_interfere_with_indexed_property_access; }
  133. ThrowCompletionOr<bool> ordinary_set_with_own_descriptor(PropertyKey const&, Value, Value, Optional<PropertyDescriptor>, CacheablePropertyMetadata* = nullptr);
  134. // 10.4.7 Immutable Prototype Exotic Objects, https://tc39.es/ecma262/#sec-immutable-prototype-exotic-objects
  135. ThrowCompletionOr<bool> set_immutable_prototype(Object* prototype);
  136. // 20.1 Object Objects, https://tc39.es/ecma262/#sec-object-objects
  137. ThrowCompletionOr<Object*> define_properties(Value properties);
  138. // 14.7.5 The for-in, for-of, and for-await-of Statements
  139. Optional<Completion> enumerate_object_properties(Function<Optional<Completion>(Value)>) const;
  140. // Implementation-specific storage abstractions
  141. Optional<ValueAndAttributes> storage_get(PropertyKey const&) const;
  142. bool storage_has(PropertyKey const&) const;
  143. void storage_set(PropertyKey const&, ValueAndAttributes const&);
  144. void storage_delete(PropertyKey const&);
  145. // Non-standard methods
  146. Value get_without_side_effects(PropertyKey const&) const;
  147. void define_direct_property(PropertyKey const& property_key, Value value, PropertyAttributes attributes) { storage_set(property_key, { value, attributes }); }
  148. void define_direct_accessor(PropertyKey const&, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes);
  149. using IntrinsicAccessor = Value (*)(Realm&);
  150. void define_intrinsic_accessor(PropertyKey const&, PropertyAttributes attributes, IntrinsicAccessor accessor);
  151. void define_native_function(Realm&, PropertyKey const&, Function<ThrowCompletionOr<Value>(VM&)>, i32 length, PropertyAttributes attributes, Optional<Bytecode::Builtin> builtin = {});
  152. void define_native_accessor(Realm&, PropertyKey const&, Function<ThrowCompletionOr<Value>(VM&)> getter, Function<ThrowCompletionOr<Value>(VM&)> setter, PropertyAttributes attributes);
  153. virtual bool is_dom_node() const { return false; }
  154. virtual bool is_function() const { return false; }
  155. virtual bool is_string_object() const { return false; }
  156. virtual bool is_global_object() const { return false; }
  157. virtual bool is_proxy_object() const { return false; }
  158. virtual bool is_native_function() const { return false; }
  159. virtual bool is_ecmascript_function_object() const { return false; }
  160. virtual bool is_iterator_record() const { return false; }
  161. virtual bool is_array_iterator() const { return false; }
  162. // B.3.7 The [[IsHTMLDDA]] Internal Slot, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot
  163. virtual bool is_htmldda() const { return false; }
  164. bool has_parameter_map() const { return m_has_parameter_map; }
  165. void set_has_parameter_map() { m_has_parameter_map = true; }
  166. virtual void visit_edges(Cell::Visitor&) override;
  167. Value get_direct(size_t index) const { return m_storage[index]; }
  168. void put_direct(size_t index, Value value) { m_storage[index] = value; }
  169. IndexedProperties const& indexed_properties() const { return m_indexed_properties; }
  170. IndexedProperties& indexed_properties() { return m_indexed_properties; }
  171. void set_indexed_property_elements(Vector<Value>&& values) { m_indexed_properties = IndexedProperties(move(values)); }
  172. Shape& shape() { return *m_shape; }
  173. Shape const& shape() const { return *m_shape; }
  174. void convert_to_prototype_if_needed();
  175. template<typename T>
  176. bool fast_is() const = delete;
  177. void set_prototype(Object*);
  178. [[nodiscard]] bool has_magical_length_property() const { return m_has_magical_length_property; }
  179. [[nodiscard]] bool is_typed_array() const { return m_is_typed_array; }
  180. void set_is_typed_array() { m_is_typed_array = true; }
  181. Object const* prototype() const { return shape().prototype(); }
  182. protected:
  183. enum class GlobalObjectTag { Tag };
  184. enum class ConstructWithoutPrototypeTag { Tag };
  185. enum class ConstructWithPrototypeTag { Tag };
  186. Object(GlobalObjectTag, Realm&, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
  187. Object(ConstructWithoutPrototypeTag, Realm&, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
  188. Object(Realm&, Object* prototype, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
  189. Object(ConstructWithPrototypeTag, Object& prototype, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
  190. explicit Object(Shape&, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
  191. // [[Extensible]]
  192. bool m_is_extensible { true };
  193. // [[ParameterMap]]
  194. bool m_has_parameter_map { false };
  195. bool m_has_magical_length_property { false };
  196. bool m_is_typed_array { false };
  197. private:
  198. void set_shape(Shape& shape) { m_shape = &shape; }
  199. Object* prototype() { return shape().prototype(); }
  200. bool m_may_interfere_with_indexed_property_access { false };
  201. // True if this object has lazily allocated intrinsic properties.
  202. bool m_has_intrinsic_accessors { false };
  203. GCPtr<Shape> m_shape;
  204. Vector<Value> m_storage;
  205. IndexedProperties m_indexed_properties;
  206. OwnPtr<Vector<PrivateElement>> m_private_elements; // [[PrivateElements]]
  207. };
  208. }