PropertyDescriptor.h 2.7 KB

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