Object.h 9.3 KB

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