Object.h 9.2 KB

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