PropertyAttributes.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  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/Format.h>
  28. #include <AK/Types.h>
  29. namespace JS {
  30. struct Attribute {
  31. enum {
  32. Configurable = 1 << 0,
  33. Enumerable = 1 << 1,
  34. Writable = 1 << 2,
  35. HasGetter = 1 << 3,
  36. HasSetter = 1 << 4,
  37. HasConfigurable = 1 << 5,
  38. HasEnumerable = 1 << 6,
  39. HasWritable = 1 << 7,
  40. };
  41. };
  42. class PropertyAttributes {
  43. public:
  44. PropertyAttributes(u8 bits = 0)
  45. {
  46. m_bits = bits;
  47. if (bits & Attribute::Configurable)
  48. m_bits |= Attribute::HasConfigurable;
  49. if (bits & Attribute::Enumerable)
  50. m_bits |= Attribute::HasEnumerable;
  51. if (bits & Attribute::Writable)
  52. m_bits |= Attribute::HasWritable;
  53. }
  54. bool is_empty() const { return !m_bits; }
  55. bool has_configurable() const { return m_bits & Attribute::HasConfigurable; }
  56. bool has_enumerable() const { return m_bits & Attribute::HasEnumerable; }
  57. bool has_writable() const { return m_bits & Attribute::HasWritable; }
  58. bool has_getter() const { return m_bits & Attribute::HasGetter; }
  59. bool has_setter() const { return m_bits & Attribute::HasSetter; }
  60. bool is_configurable() const { return m_bits & Attribute::Configurable; }
  61. bool is_enumerable() const { return m_bits & Attribute::Enumerable; }
  62. bool is_writable() const { return m_bits & Attribute::Writable; }
  63. void set_has_configurable() { m_bits |= Attribute::HasConfigurable; }
  64. void set_has_enumerable() { m_bits |= Attribute::HasEnumerable; }
  65. void set_has_writable() { m_bits |= Attribute::HasWritable; }
  66. void set_configurable() { m_bits |= Attribute::Configurable; }
  67. void set_enumerable() { m_bits |= Attribute::Enumerable; }
  68. void set_writable() { m_bits |= Attribute::Writable; }
  69. void set_has_getter() { m_bits |= Attribute::HasGetter; }
  70. void set_has_setter() { m_bits |= Attribute::HasSetter; }
  71. bool operator==(const PropertyAttributes& other) const { return m_bits == other.m_bits; }
  72. bool operator!=(const PropertyAttributes& other) const { return m_bits != other.m_bits; }
  73. u8 bits() const { return m_bits; }
  74. private:
  75. u8 m_bits;
  76. };
  77. const PropertyAttributes default_attributes = Attribute::Configurable | Attribute::Writable | Attribute::Enumerable;
  78. }
  79. namespace AK {
  80. template<>
  81. struct Formatter<JS::PropertyAttributes> : Formatter<u8> {
  82. void format(TypeErasedFormatParams& params, FormatBuilder& builder, const JS::PropertyAttributes& attributes)
  83. {
  84. Formatter<u8>::format(params, builder, attributes.bits());
  85. }
  86. };
  87. }