Object.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/HashMap.h>
  28. #include <AK/String.h>
  29. #include <LibJS/Forward.h>
  30. #include <LibJS/Runtime/Cell.h>
  31. #include <LibJS/Runtime/IndexedProperties.h>
  32. #include <LibJS/Runtime/MarkedValueList.h>
  33. #include <LibJS/Runtime/PrimitiveString.h>
  34. #include <LibJS/Runtime/PropertyName.h>
  35. #include <LibJS/Runtime/Shape.h>
  36. #include <LibJS/Runtime/Value.h>
  37. namespace JS {
  38. #define JS_OBJECT(class_, base_class) \
  39. public: \
  40. using Base = base_class; \
  41. virtual const char* class_name() const override { return #class_; } \
  42. virtual bool inherits(const StringView& class_name) const override { return class_name == #class_ || Base::inherits(class_name); }
  43. struct PropertyDescriptor {
  44. PropertyAttributes attributes;
  45. Value value;
  46. Function* getter { nullptr };
  47. Function* setter { nullptr };
  48. static PropertyDescriptor from_dictionary(Interpreter&, const Object&);
  49. bool is_accessor_descriptor() const { return getter || setter; }
  50. bool is_data_descriptor() const { return !(value.is_empty() && !attributes.has_writable()); }
  51. bool is_generic_descriptor() const { return !is_accessor_descriptor() && !is_data_descriptor(); }
  52. };
  53. class Object : public Cell {
  54. public:
  55. static Object* create_empty(GlobalObject&);
  56. explicit Object(Object& prototype);
  57. virtual void initialize(GlobalObject&) override;
  58. virtual ~Object();
  59. virtual bool inherits(const StringView& class_name) const { return class_name == this->class_name(); }
  60. enum class PropertyKind {
  61. Key,
  62. Value,
  63. KeyAndValue,
  64. };
  65. enum class GetOwnPropertyReturnType {
  66. StringOnly,
  67. SymbolOnly,
  68. };
  69. enum class PutOwnPropertyMode {
  70. Put,
  71. DefineProperty,
  72. };
  73. Shape& shape() { return *m_shape; }
  74. const Shape& shape() const { return *m_shape; }
  75. GlobalObject& global_object() const { return shape().global_object(); }
  76. virtual Value get(const PropertyName&, Value receiver = {}) const;
  77. virtual bool has_property(const PropertyName&) const;
  78. bool has_own_property(const PropertyName&) const;
  79. virtual bool put(const PropertyName&, Value, Value receiver = {});
  80. Value get_own_property(const Object& this_object, PropertyName, Value receiver) const;
  81. Value get_own_properties(const Object& this_object, PropertyKind, bool only_enumerable_properties = false, GetOwnPropertyReturnType = GetOwnPropertyReturnType::StringOnly) const;
  82. virtual Optional<PropertyDescriptor> get_own_property_descriptor(const PropertyName&) const;
  83. Value get_own_property_descriptor_object(const PropertyName&) const;
  84. virtual bool define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions = true);
  85. bool define_property(const PropertyName&, Value value, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
  86. bool define_accessor(const PropertyName&, Function& getter_or_setter, bool is_getter, PropertyAttributes attributes = default_attributes, bool throw_exceptions = true);
  87. bool define_native_function(const StringOrSymbol& property_name, AK::Function<Value(Interpreter&, GlobalObject&)>, i32 length = 0, PropertyAttributes attributes = default_attributes);
  88. bool define_native_property(const StringOrSymbol& property_name, AK::Function<Value(Interpreter&, GlobalObject&)> getter, AK::Function<void(Interpreter&, GlobalObject&, Value)> setter, PropertyAttributes attributes = default_attributes);
  89. virtual Value delete_property(const PropertyName&);
  90. virtual bool is_array() const { return false; }
  91. virtual bool is_date() const { return false; }
  92. virtual bool is_error() const { return false; }
  93. virtual bool is_function() const { return false; }
  94. virtual bool is_native_function() const { return false; }
  95. virtual bool is_bound_function() const { return false; }
  96. virtual bool is_proxy_object() const { return false; }
  97. virtual bool is_regexp_object() const { return false; }
  98. virtual bool is_boolean_object() const { return false; }
  99. virtual bool is_string_object() const { return false; }
  100. virtual bool is_number_object() const { return false; }
  101. virtual bool is_symbol_object() const { return false; }
  102. virtual bool is_bigint_object() const { return false; }
  103. virtual bool is_string_iterator_object() const { return false; }
  104. virtual bool is_array_iterator_object() const { return false; }
  105. virtual const char* class_name() const override { return "Object"; }
  106. virtual void visit_children(Cell::Visitor&) override;
  107. virtual Object* prototype();
  108. virtual const Object* prototype() const;
  109. virtual bool set_prototype(Object* prototype);
  110. bool has_prototype(const Object* prototype) const;
  111. virtual bool is_extensible() const { return m_is_extensible; }
  112. virtual bool prevent_extensions();
  113. virtual Value value_of() const { return Value(const_cast<Object*>(this)); }
  114. virtual Value to_primitive(Value::PreferredType preferred_type = Value::PreferredType::Default) const;
  115. virtual Value to_string() const;
  116. Value get_direct(size_t index) const { return m_storage[index]; }
  117. const IndexedProperties& indexed_properties() const { return m_indexed_properties; }
  118. IndexedProperties& indexed_properties() { return m_indexed_properties; }
  119. void set_indexed_property_elements(Vector<Value>&& values) { m_indexed_properties = IndexedProperties(move(values)); }
  120. Value invoke(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments = {});
  121. protected:
  122. enum class GlobalObjectTag { Tag };
  123. enum class ConstructWithoutPrototypeTag { Tag };
  124. explicit Object(GlobalObjectTag);
  125. Object(ConstructWithoutPrototypeTag, GlobalObject&);
  126. private:
  127. virtual Value get_by_index(u32 property_index) const;
  128. virtual bool put_by_index(u32 property_index, Value);
  129. bool put_own_property(Object& this_object, const StringOrSymbol& property_name, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
  130. bool put_own_property_by_index(Object& this_object, u32 property_index, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
  131. Value call_native_property_getter(Object* this_object, Value property) const;
  132. void call_native_property_setter(Object* this_object, Value property, Value) const;
  133. void set_shape(Shape&);
  134. void ensure_shape_is_unique();
  135. bool m_is_extensible { true };
  136. Shape* m_shape { nullptr };
  137. Vector<Value> m_storage;
  138. IndexedProperties m_indexed_properties;
  139. };
  140. }