PropertyAttributes.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Format.h>
  8. #include <AK/Types.h>
  9. namespace JS {
  10. struct Attribute {
  11. enum {
  12. Configurable = 1 << 0,
  13. Enumerable = 1 << 1,
  14. Writable = 1 << 2,
  15. HasGetter = 1 << 3,
  16. HasSetter = 1 << 4,
  17. HasConfigurable = 1 << 5,
  18. HasEnumerable = 1 << 6,
  19. HasWritable = 1 << 7,
  20. };
  21. };
  22. class PropertyAttributes {
  23. public:
  24. PropertyAttributes(u8 bits = 0)
  25. {
  26. m_bits = bits;
  27. if (bits & Attribute::Configurable)
  28. m_bits |= Attribute::HasConfigurable;
  29. if (bits & Attribute::Enumerable)
  30. m_bits |= Attribute::HasEnumerable;
  31. if (bits & Attribute::Writable)
  32. m_bits |= Attribute::HasWritable;
  33. }
  34. bool is_empty() const { return !m_bits; }
  35. bool has_configurable() const { return m_bits & Attribute::HasConfigurable; }
  36. bool has_enumerable() const { return m_bits & Attribute::HasEnumerable; }
  37. bool has_writable() const { return m_bits & Attribute::HasWritable; }
  38. bool has_getter() const { return m_bits & Attribute::HasGetter; }
  39. bool has_setter() const { return m_bits & Attribute::HasSetter; }
  40. bool is_configurable() const { return m_bits & Attribute::Configurable; }
  41. bool is_enumerable() const { return m_bits & Attribute::Enumerable; }
  42. bool is_writable() const { return m_bits & Attribute::Writable; }
  43. void set_has_configurable() { m_bits |= Attribute::HasConfigurable; }
  44. void set_has_enumerable() { m_bits |= Attribute::HasEnumerable; }
  45. void set_has_writable() { m_bits |= Attribute::HasWritable; }
  46. void set_configurable() { m_bits |= Attribute::Configurable; }
  47. void set_enumerable() { m_bits |= Attribute::Enumerable; }
  48. void set_writable() { m_bits |= Attribute::Writable; }
  49. void set_has_getter() { m_bits |= Attribute::HasGetter; }
  50. void set_has_setter() { m_bits |= Attribute::HasSetter; }
  51. bool operator==(const PropertyAttributes& other) const { return m_bits == other.m_bits; }
  52. bool operator!=(const PropertyAttributes& other) const { return m_bits != other.m_bits; }
  53. PropertyAttributes overwrite(PropertyAttributes attr) const;
  54. u8 bits() const { return m_bits; }
  55. private:
  56. u8 m_bits;
  57. };
  58. const PropertyAttributes default_attributes = Attribute::Configurable | Attribute::Writable | Attribute::Enumerable;
  59. }
  60. namespace AK {
  61. template<>
  62. struct Formatter<JS::PropertyAttributes> : Formatter<u8> {
  63. void format(FormatBuilder& builder, const JS::PropertyAttributes& attributes)
  64. {
  65. Formatter<u8>::format(builder, attributes.bits());
  66. }
  67. };
  68. }