PlatformObject.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Weakable.h>
  8. #include <LibJS/Runtime/Object.h>
  9. #include <LibWeb/Forward.h>
  10. namespace Web::Bindings {
  11. #define WEB_PLATFORM_OBJECT(class_, base_class) \
  12. JS_OBJECT(class_, base_class) \
  13. virtual bool implements_interface(String const& interface) const override \
  14. { \
  15. if (interface == #class_) \
  16. return true; \
  17. return Base::implements_interface(interface); \
  18. }
  19. // https://webidl.spec.whatwg.org/#dfn-platform-object
  20. class PlatformObject : public JS::Object {
  21. JS_OBJECT(PlatformObject, JS::Object);
  22. public:
  23. virtual ~PlatformObject() override;
  24. JS::Realm& realm() const;
  25. // https://webidl.spec.whatwg.org/#implements
  26. // This is implemented by overrides that get generated by the WEB_PLATFORM_OBJECT macro.
  27. [[nodiscard]] virtual bool implements_interface(String const&) const { return false; }
  28. // ^JS::Object
  29. virtual JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyKey const&) const override;
  30. virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value, JS::Value, JS::CacheablePropertyMetadata* = nullptr) override;
  31. virtual JS::ThrowCompletionOr<bool> internal_define_own_property(JS::PropertyKey const&, JS::PropertyDescriptor const&, Optional<JS::PropertyDescriptor>* precomputed_get_own_property = nullptr) override;
  32. virtual JS::ThrowCompletionOr<bool> internal_delete(JS::PropertyKey const&) override;
  33. virtual JS::ThrowCompletionOr<bool> internal_prevent_extensions() override;
  34. virtual JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> internal_own_property_keys() const override;
  35. JS::ThrowCompletionOr<bool> is_named_property_exposed_on_object(JS::PropertyKey const&) const;
  36. protected:
  37. explicit PlatformObject(JS::Realm&, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
  38. explicit PlatformObject(JS::Object& prototype, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
  39. struct LegacyPlatformObjectFlags {
  40. u16 supports_indexed_properties : 1 = false;
  41. u16 supports_named_properties : 1 = false;
  42. u16 has_indexed_property_setter : 1 = false;
  43. u16 has_named_property_setter : 1 = false;
  44. u16 has_named_property_deleter : 1 = false;
  45. u16 has_legacy_unenumerable_named_properties_interface_extended_attribute : 1 = false;
  46. u16 has_legacy_override_built_ins_interface_extended_attribute : 1 = false;
  47. u16 has_global_interface_extended_attribute : 1 = false;
  48. u16 indexed_property_setter_has_identifier : 1 = false;
  49. u16 named_property_setter_has_identifier : 1 = false;
  50. u16 named_property_deleter_has_identifier : 1 = false;
  51. };
  52. Optional<LegacyPlatformObjectFlags> m_legacy_platform_object_flags = {};
  53. enum class IgnoreNamedProps {
  54. No,
  55. Yes,
  56. };
  57. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> legacy_platform_object_get_own_property(JS::PropertyKey const&, IgnoreNamedProps ignore_named_props) const;
  58. virtual Optional<JS::Value> item_value(size_t index) const;
  59. virtual JS::Value named_item_value(FlyString const& name) const;
  60. virtual Vector<FlyString> supported_property_names() const;
  61. virtual bool is_supported_property_name(FlyString const&) const;
  62. bool is_supported_property_index(u32) const;
  63. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
  64. // NOTE: This is only used if named_property_setter_has_identifier returns false, otherwise set_value_of_named_property is used instead.
  65. virtual WebIDL::ExceptionOr<void> set_value_of_new_named_property(String const&, JS::Value);
  66. virtual WebIDL::ExceptionOr<void> set_value_of_existing_named_property(String const&, JS::Value);
  67. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
  68. // 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.
  69. virtual WebIDL::ExceptionOr<void> set_value_of_named_property(String const&, JS::Value);
  70. // NOTE: These will crash if you make has_indexed_property_setter return true but do not override these methods.
  71. // NOTE: This is only used if indexed_property_setter_has_identifier returns false, otherwise set_value_of_indexed_property is used instead.
  72. virtual WebIDL::ExceptionOr<void> set_value_of_new_indexed_property(u32, JS::Value);
  73. virtual WebIDL::ExceptionOr<void> set_value_of_existing_indexed_property(u32, JS::Value);
  74. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
  75. // NOTE: This is only used if indexed_property_setter_has_identifier returns true, otherwise set_value_of_{new,existing}_indexed_property is used instead.
  76. virtual WebIDL::ExceptionOr<void> set_value_of_indexed_property(u32, JS::Value);
  77. enum class DidDeletionFail {
  78. // If the named property deleter has an identifier, but does not return a boolean.
  79. // This is done because we don't know the return type of the deleter outside of the IDL generator.
  80. NotRelevant,
  81. No,
  82. Yes,
  83. };
  84. // NOTE: This will crash if you make has_named_property_deleter return true but do not override this method.
  85. virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(String const&);
  86. private:
  87. WebIDL::ExceptionOr<void> invoke_indexed_property_setter(JS::PropertyKey const&, JS::Value);
  88. WebIDL::ExceptionOr<void> invoke_named_property_setter(FlyString const&, JS::Value);
  89. };
  90. }