PropertyDescriptor.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <LibJS/Forward.h>
  9. #include <LibJS/Runtime/Value.h>
  10. namespace JS {
  11. // 6.2.5 The Property Descriptor Specification Type, https://tc39.es/ecma262/#sec-property-descriptor-specification-type
  12. Value from_property_descriptor(GlobalObject&, Optional<PropertyDescriptor> const&);
  13. PropertyDescriptor to_property_descriptor(GlobalObject&, Value);
  14. class PropertyDescriptor {
  15. public:
  16. [[nodiscard]] bool is_accessor_descriptor() const;
  17. [[nodiscard]] bool is_data_descriptor() const;
  18. [[nodiscard]] bool is_generic_descriptor() const;
  19. [[nodiscard]] PropertyAttributes attributes() const;
  20. void complete();
  21. // Not a standard abstract operation, but "If every field in Desc is absent".
  22. [[nodiscard]] bool is_empty() const
  23. {
  24. return !value.has_value() && !get.has_value() && !set.has_value() && !writable.has_value() && !enumerable.has_value() && !configurable.has_value();
  25. }
  26. Optional<Value> value {};
  27. Optional<FunctionObject*> get {};
  28. Optional<FunctionObject*> set {};
  29. Optional<bool> writable {};
  30. Optional<bool> enumerable {};
  31. Optional<bool> configurable {};
  32. };
  33. }
  34. namespace AK {
  35. template<>
  36. struct Formatter<JS::PropertyDescriptor> : Formatter<StringView> {
  37. void format(FormatBuilder& builder, JS::PropertyDescriptor const& property_descriptor)
  38. {
  39. Vector<String> parts;
  40. if (property_descriptor.value.has_value())
  41. parts.append(String::formatted("[[Value]]: {}", property_descriptor.value->to_string_without_side_effects()));
  42. if (property_descriptor.get.has_value())
  43. parts.append(String::formatted("[[Get]]: JS::Function* @ {:p}", *property_descriptor.get));
  44. if (property_descriptor.set.has_value())
  45. parts.append(String::formatted("[[Set]]: JS::Function* @ {:p}", *property_descriptor.set));
  46. if (property_descriptor.writable.has_value())
  47. parts.append(String::formatted("[[Writable]]: {}", *property_descriptor.writable));
  48. if (property_descriptor.enumerable.has_value())
  49. parts.append(String::formatted("[[Enumerable]]: {}", *property_descriptor.enumerable));
  50. if (property_descriptor.configurable.has_value())
  51. parts.append(String::formatted("[[Configurable]]: {}", *property_descriptor.configurable));
  52. Formatter<StringView>::format(builder, String::formatted("PropertyDescriptor {{ {} }}", String::join(", ", parts)));
  53. }
  54. };
  55. }