Object.h 9.0 KB

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