Object.h 11 KB

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