Object.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/PrimitiveString.h>
  32. #include <LibJS/Runtime/PropertyName.h>
  33. #include <LibJS/Runtime/Shape.h>
  34. #include <LibJS/Runtime/Value.h>
  35. namespace JS {
  36. const u8 default_attributes = Attribute::Configurable | Attribute::Writable | Attribute::Enumerable;
  37. class Object : public Cell {
  38. public:
  39. static Object* create_empty(Interpreter&, GlobalObject&);
  40. explicit Object(Object* prototype);
  41. virtual ~Object();
  42. enum class GetOwnPropertyMode {
  43. Key,
  44. Value,
  45. KeyAndValue,
  46. };
  47. enum class PutOwnPropertyMode {
  48. Put,
  49. DefineProperty,
  50. };
  51. Shape& shape() { return *m_shape; }
  52. const Shape& shape() const { return *m_shape; }
  53. Value delete_property(PropertyName);
  54. virtual Value get_by_index(i32 property_index) const;
  55. Value get(const FlyString& property_name) const;
  56. Value get(PropertyName) const;
  57. virtual bool put_by_index(i32 property_index, Value, u8 attributes = default_attributes);
  58. bool put(const FlyString& property_name, Value, u8 attributes = default_attributes);
  59. bool put(PropertyName, Value, u8 attributes = default_attributes);
  60. Value get_own_property(const Object& this_object, const FlyString& property_name) const;
  61. Value get_own_properties(const Object& this_object, GetOwnPropertyMode, u8 attributes = Attribute::Configurable | Attribute::Enumerable | Attribute::Writable) const;
  62. Value get_own_property_descriptor(const FlyString& property_name) const;
  63. bool define_property(const FlyString& property_name, const Object& descriptor, bool throw_exceptions = true);
  64. bool put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value, PutOwnPropertyMode, bool throw_exceptions = true);
  65. bool put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>, i32 length = 0, u8 attribute = default_attributes);
  66. bool put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attribute = default_attributes);
  67. virtual bool is_array() const { return false; }
  68. virtual bool is_boolean() const { return false; }
  69. virtual bool is_date() const { return false; }
  70. virtual bool is_error() const { return false; }
  71. virtual bool is_function() const { return false; }
  72. virtual bool is_native_function() const { return false; }
  73. virtual bool is_bound_function() const { return false; }
  74. virtual bool is_native_property() const { return false; }
  75. virtual bool is_string_object() const { return false; }
  76. virtual bool is_symbol_object() const { return false; }
  77. virtual const char* class_name() const override { return "Object"; }
  78. virtual void visit_children(Cell::Visitor&) override;
  79. Object* prototype();
  80. const Object* prototype() const;
  81. void set_prototype(Object*);
  82. bool has_prototype(const Object* prototype) const;
  83. bool has_property(const FlyString& property_name) const;
  84. bool has_own_property(const FlyString& property_name) const;
  85. enum class PreferredType {
  86. Default,
  87. String,
  88. Number,
  89. };
  90. virtual Value value_of() const { return Value(const_cast<Object*>(this)); }
  91. virtual Value to_primitive(PreferredType preferred_type = PreferredType::Default) const;
  92. virtual Value to_string() const;
  93. Value get_direct(size_t index) const { return m_storage[index]; }
  94. const Vector<Value>& elements() const { return m_elements; }
  95. Vector<Value>& elements() { return m_elements; }
  96. private:
  97. void set_shape(Shape&);
  98. void ensure_shape_is_unique();
  99. Shape* m_shape { nullptr };
  100. Vector<Value> m_storage;
  101. Vector<Value> m_elements;
  102. };
  103. }