Object.h 9.0 KB

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