Object.h 8.0 KB

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