PropertyDescriptor.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. };
  34. }
  35. namespace AK {
  36. template<>
  37. struct Formatter<JS::PropertyDescriptor> : Formatter<StringView> {
  38. ErrorOr<void> format(FormatBuilder& builder, JS::PropertyDescriptor const& property_descriptor)
  39. {
  40. Vector<String> parts;
  41. if (property_descriptor.value.has_value())
  42. TRY(parts.try_append(TRY(String::formatted("[[Value]]: {}", TRY(property_descriptor.value->to_string_without_side_effects())))));
  43. if (property_descriptor.get.has_value())
  44. TRY(parts.try_append(TRY(String::formatted("[[Get]]: JS::Function* @ {:p}", property_descriptor.get->ptr()))));
  45. if (property_descriptor.set.has_value())
  46. TRY(parts.try_append(TRY(String::formatted("[[Set]]: JS::Function* @ {:p}", property_descriptor.set->ptr()))));
  47. if (property_descriptor.writable.has_value())
  48. TRY(parts.try_append(TRY(String::formatted("[[Writable]]: {}", *property_descriptor.writable))));
  49. if (property_descriptor.enumerable.has_value())
  50. TRY(parts.try_append(TRY(String::formatted("[[Enumerable]]: {}", *property_descriptor.enumerable))));
  51. if (property_descriptor.configurable.has_value())
  52. TRY(parts.try_append(TRY(String::formatted("[[Configurable]]: {}", *property_descriptor.configurable))));
  53. return Formatter<StringView>::format(builder, TRY(String::formatted("PropertyDescriptor {{ {} }}", TRY(String::join(", "sv, parts)))));
  54. }
  55. };
  56. }