Object.h 9.4 KB

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