Object.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. class Object : public Cell {
  36. JS_CELL(Object, Cell);
  37. public:
  38. static NonnullGCPtr<Object> create(Realm&, Object* prototype);
  39. virtual ThrowCompletionOr<void> initialize(Realm&) override;
  40. virtual ~Object();
  41. enum class PropertyKind {
  42. Key,
  43. Value,
  44. KeyAndValue,
  45. };
  46. enum class IntegrityLevel {
  47. Sealed,
  48. Frozen,
  49. };
  50. enum class ShouldThrowExceptions {
  51. No,
  52. Yes,
  53. };
  54. // Please DO NOT make up your own non-standard methods unless you
  55. // have a very good reason to do so. If any object abstract
  56. // operation from the spec is missing, add it instead.
  57. // Functionality for implementation details like shapes and
  58. // property storage are obviously exempt from this rule :^)
  59. //
  60. // Methods named [[Foo]]() in the spec are named internal_foo()
  61. // here, as they are "The [[Foo]] internal method of a ... object".
  62. // They must be virtual and may be overridden. All other methods
  63. // follow the regular PascalCase name converted to camel_case
  64. // naming convention and must not be virtual.
  65. // 7.1 Type Conversion, https://tc39.es/ecma262/#sec-type-conversion
  66. ThrowCompletionOr<Value> ordinary_to_primitive(Value::PreferredType preferred_type) const;
  67. // 7.2 Testing and Comparison Operations, https://tc39.es/ecma262/#sec-testing-and-comparison-operations
  68. ThrowCompletionOr<bool> is_extensible() const;
  69. // 7.3 Operations on Objects, https://tc39.es/ecma262/#sec-operations-on-objects
  70. ThrowCompletionOr<Value> get(PropertyKey const&) const;
  71. ThrowCompletionOr<void> set(PropertyKey const&, Value, ShouldThrowExceptions);
  72. ThrowCompletionOr<bool> create_data_property(PropertyKey const&, Value);
  73. void create_method_property(PropertyKey const&, Value);
  74. ThrowCompletionOr<bool> create_data_property_or_throw(PropertyKey const&, Value);
  75. void create_non_enumerable_data_property_or_throw(PropertyKey const&, Value);
  76. ThrowCompletionOr<void> define_property_or_throw(PropertyKey const&, PropertyDescriptor const&);
  77. ThrowCompletionOr<void> delete_property_or_throw(PropertyKey const&);
  78. ThrowCompletionOr<bool> has_property(PropertyKey const&) const;
  79. ThrowCompletionOr<bool> has_own_property(PropertyKey const&) const;
  80. ThrowCompletionOr<bool> set_integrity_level(IntegrityLevel);
  81. ThrowCompletionOr<bool> test_integrity_level(IntegrityLevel) const;
  82. ThrowCompletionOr<MarkedVector<Value>> enumerable_own_property_names(PropertyKind kind) const;
  83. ThrowCompletionOr<void> copy_data_properties(VM&, Value source, HashTable<PropertyKey> const& seen_names);
  84. PrivateElement* private_element_find(PrivateName const& name);
  85. ThrowCompletionOr<void> private_field_add(PrivateName const& name, Value value);
  86. ThrowCompletionOr<void> private_method_or_accessor_add(PrivateElement element);
  87. ThrowCompletionOr<Value> private_get(PrivateName const& name);
  88. ThrowCompletionOr<void> private_set(PrivateName const& name, Value value);
  89. ThrowCompletionOr<void> define_field(ClassFieldDefinition const&);
  90. ThrowCompletionOr<void> initialize_instance_elements(ECMAScriptFunctionObject& constructor);
  91. // 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
  92. virtual ThrowCompletionOr<Object*> internal_get_prototype_of() const;
  93. virtual ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype);
  94. virtual ThrowCompletionOr<bool> internal_is_extensible() const;
  95. virtual ThrowCompletionOr<bool> internal_prevent_extensions();
  96. virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const;
  97. virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&);
  98. virtual ThrowCompletionOr<bool> internal_has_property(PropertyKey const&) const;
  99. virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver) const;
  100. virtual ThrowCompletionOr<bool> internal_set(PropertyKey const&, Value value, Value receiver);
  101. virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&);
  102. virtual ThrowCompletionOr<MarkedVector<Value>> internal_own_property_keys() const;
  103. ThrowCompletionOr<bool> ordinary_set_with_own_descriptor(PropertyKey const&, Value, Value, Optional<PropertyDescriptor>);
  104. // 10.4.7 Immutable Prototype Exotic Objects, https://tc39.es/ecma262/#sec-immutable-prototype-exotic-objects
  105. ThrowCompletionOr<bool> set_immutable_prototype(Object* prototype);
  106. // 20.1 Object Objects, https://tc39.es/ecma262/#sec-object-objects
  107. ThrowCompletionOr<Object*> define_properties(Value properties);
  108. // 14.7.5 The for-in, for-of, and for-await-of Statements
  109. Optional<Completion> enumerate_object_properties(Function<Optional<Completion>(Value)>) const;
  110. // Implementation-specific storage abstractions
  111. Optional<ValueAndAttributes> storage_get(PropertyKey const&) const;
  112. bool storage_has(PropertyKey const&) const;
  113. void storage_set(PropertyKey const&, ValueAndAttributes const&);
  114. void storage_delete(PropertyKey const&);
  115. // Non-standard methods
  116. Value get_without_side_effects(PropertyKey const&) const;
  117. void define_direct_property(PropertyKey const& property_key, Value value, PropertyAttributes attributes) { storage_set(property_key, { value, attributes }); };
  118. void define_direct_accessor(PropertyKey const&, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes);
  119. using IntrinsicAccessor = Value (*)(Realm&);
  120. virtual void define_intrinsic_accessor(PropertyKey const&, PropertyAttributes attributes, IntrinsicAccessor accessor);
  121. void define_native_function(Realm&, PropertyKey const&, SafeFunction<ThrowCompletionOr<Value>(VM&)>, i32 length, PropertyAttributes attributes);
  122. void define_native_accessor(Realm&, PropertyKey const&, SafeFunction<ThrowCompletionOr<Value>(VM&)> getter, SafeFunction<ThrowCompletionOr<Value>(VM&)> setter, PropertyAttributes attributes);
  123. virtual bool is_function() const { return false; }
  124. virtual bool is_typed_array() const { return false; }
  125. virtual bool is_string_object() const { return false; }
  126. virtual bool is_global_object() const { return false; }
  127. virtual bool is_proxy_object() const { return false; }
  128. virtual bool is_native_function() const { return false; }
  129. virtual bool is_ecmascript_function_object() const { return false; }
  130. // B.3.7 The [[IsHTMLDDA]] Internal Slot, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot
  131. virtual bool is_htmldda() const { return false; }
  132. bool has_parameter_map() const { return m_has_parameter_map; }
  133. void set_has_parameter_map() { m_has_parameter_map = true; }
  134. virtual void visit_edges(Cell::Visitor&) override;
  135. Value get_direct(size_t index) const { return m_storage[index]; }
  136. IndexedProperties const& indexed_properties() const { return m_indexed_properties; }
  137. IndexedProperties& indexed_properties() { return m_indexed_properties; }
  138. void set_indexed_property_elements(Vector<Value>&& values) { m_indexed_properties = IndexedProperties(move(values)); }
  139. Shape& shape() { return *m_shape; }
  140. Shape const& shape() const { return *m_shape; }
  141. void ensure_shape_is_unique();
  142. template<typename T>
  143. bool fast_is() const = delete;
  144. void set_prototype(Object*);
  145. protected:
  146. enum class GlobalObjectTag { Tag };
  147. enum class ConstructWithoutPrototypeTag { Tag };
  148. enum class ConstructWithPrototypeTag { Tag };
  149. Object(GlobalObjectTag, Realm&);
  150. Object(ConstructWithoutPrototypeTag, Realm&);
  151. Object(Realm&, Object* prototype);
  152. Object(ConstructWithPrototypeTag, Object& prototype);
  153. explicit Object(Shape&);
  154. // [[Extensible]]
  155. bool m_is_extensible { true };
  156. // [[ParameterMap]]
  157. bool m_has_parameter_map { false };
  158. private:
  159. void set_shape(Shape& shape) { m_shape = &shape; }
  160. Object* prototype() { return shape().prototype(); }
  161. Object const* prototype() const { return shape().prototype(); }
  162. // True if this object has lazily allocated intrinsic properties.
  163. bool m_has_intrinsic_accessors { false };
  164. GCPtr<Shape> m_shape;
  165. Vector<Value> m_storage;
  166. IndexedProperties m_indexed_properties;
  167. OwnPtr<Vector<PrivateElement>> m_private_elements; // [[PrivateElements]]
  168. };
  169. }