PlatformObject.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.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
  21. : public JS::Object
  22. , public Weakable<PlatformObject> {
  23. JS_OBJECT(PlatformObject, JS::Object);
  24. public:
  25. virtual ~PlatformObject() override;
  26. JS::Realm& realm() const;
  27. // FIXME: This should return a type that works in both window and worker contexts.
  28. HTML::Window& global_object() const;
  29. // https://webidl.spec.whatwg.org/#implements
  30. // This is implemented by overrides that get generated by the WEB_PLATFORM_OBJECT macro.
  31. [[nodiscard]] virtual bool implements_interface(String const&) const { return false; }
  32. // ^JS::Object
  33. virtual JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyKey const&) const override;
  34. virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value, JS::Value, JS::CacheablePropertyMetadata* = nullptr) override;
  35. virtual JS::ThrowCompletionOr<bool> internal_define_own_property(JS::PropertyKey const&, JS::PropertyDescriptor const&) override;
  36. virtual JS::ThrowCompletionOr<bool> internal_delete(JS::PropertyKey const&) override;
  37. virtual JS::ThrowCompletionOr<bool> internal_prevent_extensions() override;
  38. virtual JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> internal_own_property_keys() const override;
  39. JS::ThrowCompletionOr<bool> is_named_property_exposed_on_object(JS::PropertyKey const&) const;
  40. protected:
  41. explicit PlatformObject(JS::Realm&, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
  42. explicit PlatformObject(JS::Object& prototype, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
  43. struct LegacyPlatformObjectFlags {
  44. u16 supports_indexed_properties : 1 = false;
  45. u16 supports_named_properties : 1 = false;
  46. u16 has_indexed_property_setter : 1 = false;
  47. u16 has_named_property_setter : 1 = false;
  48. u16 has_named_property_deleter : 1 = false;
  49. u16 has_legacy_unenumerable_named_properties_interface_extended_attribute : 1 = false;
  50. u16 has_legacy_override_built_ins_interface_extended_attribute : 1 = false;
  51. u16 has_global_interface_extended_attribute : 1 = false;
  52. u16 indexed_property_setter_has_identifier : 1 = false;
  53. u16 named_property_setter_has_identifier : 1 = false;
  54. u16 named_property_deleter_has_identifier : 1 = false;
  55. };
  56. Optional<LegacyPlatformObjectFlags> m_legacy_platform_object_flags = {};
  57. enum class IgnoreNamedProps {
  58. No,
  59. Yes,
  60. };
  61. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> legacy_platform_object_get_own_property(JS::PropertyKey const&, IgnoreNamedProps ignore_named_props) const;
  62. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const;
  63. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const& name) const;
  64. virtual Vector<FlyString> supported_property_names() const;
  65. virtual bool is_supported_property_index(u32) const;
  66. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
  67. // NOTE: This is only used if named_property_setter_has_identifier returns false, otherwise set_value_of_named_property is used instead.
  68. virtual WebIDL::ExceptionOr<void> set_value_of_new_named_property(String const&, JS::Value);
  69. virtual WebIDL::ExceptionOr<void> set_value_of_existing_named_property(String const&, JS::Value);
  70. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
  71. // 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.
  72. virtual WebIDL::ExceptionOr<void> set_value_of_named_property(String const&, JS::Value);
  73. // NOTE: These will crash if you make has_indexed_property_setter return true but do not override these methods.
  74. // NOTE: This is only used if indexed_property_setter_has_identifier returns false, otherwise set_value_of_indexed_property is used instead.
  75. virtual WebIDL::ExceptionOr<void> set_value_of_new_indexed_property(u32, JS::Value);
  76. virtual WebIDL::ExceptionOr<void> set_value_of_existing_indexed_property(u32, JS::Value);
  77. // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods.
  78. // NOTE: This is only used if indexed_property_setter_has_identifier returns true, otherwise set_value_of_{new,existing}_indexed_property is used instead.
  79. virtual WebIDL::ExceptionOr<void> set_value_of_indexed_property(u32, JS::Value);
  80. enum class DidDeletionFail {
  81. // If the named property deleter has an identifier, but does not return a boolean.
  82. // This is done because we don't know the return type of the deleter outside of the IDL generator.
  83. NotRelevant,
  84. No,
  85. Yes,
  86. };
  87. // NOTE: This will crash if you make has_named_property_deleter return true but do not override this method.
  88. virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(String const&);
  89. private:
  90. WebIDL::ExceptionOr<void> invoke_indexed_property_setter(JS::PropertyKey const&, JS::Value);
  91. WebIDL::ExceptionOr<void> invoke_named_property_setter(String const&, JS::Value);
  92. };
  93. }