PropertyDescriptor.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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() && !unimplemented.has_value();
  26. }
  27. Optional<Value> value {};
  28. Optional<GC::Ptr<FunctionObject>> get {};
  29. Optional<GC::Ptr<FunctionObject>> set {};
  30. Optional<bool> writable {};
  31. Optional<bool> enumerable {};
  32. Optional<bool> configurable {};
  33. Optional<bool> unimplemented {};
  34. Optional<u32> property_offset {};
  35. };
  36. }
  37. namespace AK {
  38. template<>
  39. struct Formatter<JS::PropertyDescriptor> : Formatter<StringView> {
  40. ErrorOr<void> format(FormatBuilder& builder, JS::PropertyDescriptor const& property_descriptor)
  41. {
  42. Vector<String> parts;
  43. if (property_descriptor.value.has_value())
  44. TRY(parts.try_append(TRY(String::formatted("[[Value]]: {}", property_descriptor.value->to_string_without_side_effects()))));
  45. if (property_descriptor.get.has_value())
  46. TRY(parts.try_append(TRY(String::formatted("[[Get]]: JS::Function* @ {:p}", property_descriptor.get->ptr()))));
  47. if (property_descriptor.set.has_value())
  48. TRY(parts.try_append(TRY(String::formatted("[[Set]]: JS::Function* @ {:p}", property_descriptor.set->ptr()))));
  49. if (property_descriptor.writable.has_value())
  50. TRY(parts.try_append(TRY(String::formatted("[[Writable]]: {}", *property_descriptor.writable))));
  51. if (property_descriptor.enumerable.has_value())
  52. TRY(parts.try_append(TRY(String::formatted("[[Enumerable]]: {}", *property_descriptor.enumerable))));
  53. if (property_descriptor.configurable.has_value())
  54. TRY(parts.try_append(TRY(String::formatted("[[Configurable]]: {}", *property_descriptor.configurable))));
  55. if (property_descriptor.unimplemented.has_value())
  56. TRY(parts.try_append(TRY(String::formatted("[[Unimplemented]]: {}", *property_descriptor.unimplemented))));
  57. return Formatter<StringView>::format(builder, TRY(String::formatted("PropertyDescriptor {{ {} }}", TRY(String::join(", "sv, parts)))));
  58. }
  59. };
  60. }