LegacyPlatformObject.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibWeb/Bindings/PlatformObject.h>
  9. #include <LibWeb/WebIDL/ExceptionOr.h>
  10. namespace Web::Bindings {
  11. // https://webidl.spec.whatwg.org/#dfn-legacy-platform-object
  12. class LegacyPlatformObject : public PlatformObject {
  13. WEB_PLATFORM_OBJECT(LegacyPlatformObject, PlatformObject);
  14. public:
  15. virtual ~LegacyPlatformObject() override;
  16. virtual JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyKey const&) const override;
  17. virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value, JS::Value) override;
  18. virtual JS::ThrowCompletionOr<bool> internal_define_own_property(JS::PropertyKey const&, JS::PropertyDescriptor const&) override;
  19. virtual JS::ThrowCompletionOr<bool> internal_delete(JS::PropertyKey const&) override;
  20. virtual JS::ThrowCompletionOr<bool> internal_prevent_extensions() override;
  21. virtual JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> internal_own_property_keys() const override;
  22. JS::ThrowCompletionOr<bool> is_named_property_exposed_on_object(JS::PropertyKey const&) const;
  23. enum class IgnoreNamedProps {
  24. No,
  25. Yes,
  26. };
  27. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> legacy_platform_object_get_own_property(JS::PropertyKey const&, IgnoreNamedProps ignore_named_props) const;
  28. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const;
  29. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(DeprecatedFlyString const& name) const;
  30. virtual Vector<DeprecatedString> supported_property_names() const;
  31. virtual bool is_supported_property_index(u32) const;
  32. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
  33. // NOTE: This is only used if named_property_setter_has_identifier returns false, otherwise set_value_of_named_property is used instead.
  34. virtual WebIDL::ExceptionOr<void> set_value_of_new_named_property(DeprecatedString const&, JS::Value) { VERIFY_NOT_REACHED(); }
  35. virtual WebIDL::ExceptionOr<void> set_value_of_existing_named_property(DeprecatedString const&, JS::Value) { VERIFY_NOT_REACHED(); }
  36. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
  37. // NOTE: This is only used if you make named_property_setter_has_identifier return true, otherwise set_value_of_{new,existing}_named_property is used instead.
  38. virtual WebIDL::ExceptionOr<void> set_value_of_named_property(DeprecatedString const&, JS::Value) { VERIFY_NOT_REACHED(); }
  39. // NOTE: These will crash if you make has_indexed_property_setter return true but do not override these methods.
  40. // NOTE: This is only used if indexed_property_setter_has_identifier returns false, otherwise set_value_of_indexed_property is used instead.
  41. virtual WebIDL::ExceptionOr<void> set_value_of_new_indexed_property(u32, JS::Value) { VERIFY_NOT_REACHED(); }
  42. virtual WebIDL::ExceptionOr<void> set_value_of_existing_indexed_property(u32, JS::Value) { VERIFY_NOT_REACHED(); }
  43. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
  44. // NOTE: This is only used if indexed_property_setter_has_identifier returns true, otherwise set_value_of_{new,existing}_indexed_property is used instead.
  45. virtual WebIDL::ExceptionOr<void> set_value_of_indexed_property(u32, JS::Value) { VERIFY_NOT_REACHED(); }
  46. enum class DidDeletionFail {
  47. // If the named property deleter has an identifier, but does not return a boolean.
  48. // This is done because we don't know the return type of the deleter outside of the IDL generator.
  49. NotRelevant,
  50. No,
  51. Yes,
  52. };
  53. // NOTE: This will crash if you make has_named_property_deleter return true but do not override this method.
  54. virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(DeprecatedString const&) { VERIFY_NOT_REACHED(); }
  55. protected:
  56. explicit LegacyPlatformObject(JS::Realm& realm);
  57. // NOTE: These two can also be seen as "has x property getter"
  58. virtual bool supports_indexed_properties() const = 0;
  59. virtual bool supports_named_properties() const = 0;
  60. virtual bool has_indexed_property_setter() const = 0;
  61. virtual bool has_named_property_setter() const = 0;
  62. virtual bool has_named_property_deleter() const = 0;
  63. virtual bool has_legacy_override_built_ins_interface_extended_attribute() const = 0;
  64. virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const = 0;
  65. virtual bool has_global_interface_extended_attribute() const = 0;
  66. virtual bool indexed_property_setter_has_identifier() const = 0;
  67. virtual bool named_property_setter_has_identifier() const = 0;
  68. virtual bool named_property_deleter_has_identifier() const = 0;
  69. private:
  70. WebIDL::ExceptionOr<void> invoke_indexed_property_setter(JS::PropertyKey const&, JS::Value);
  71. WebIDL::ExceptionOr<void> invoke_named_property_setter(DeprecatedString const&, JS::Value);
  72. };
  73. }