Object.h 7.9 KB

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