PropertyAttributes.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/Types.h>
  28. #include <AK/LogStream.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 LogStream& operator<<(const LogStream& stream, const PropertyAttributes& attributes);
  78. const PropertyAttributes default_attributes = Attribute::Configurable | Attribute::Writable | Attribute::Enumerable;
  79. }