Intrinsics.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <AK/HashMap.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibJS/Heap/Cell.h>
  11. #include <LibJS/Heap/Heap.h>
  12. #include <LibJS/Runtime/VM.h>
  13. #include <LibWeb/Bindings/HostDefined.h>
  14. namespace Web::Bindings {
  15. class Intrinsics final : public JS::Cell {
  16. JS_CELL(Intrinsics, JS::Cell);
  17. public:
  18. Intrinsics(JS::Realm& realm)
  19. : m_realm(realm)
  20. {
  21. }
  22. template<typename NamespaceType>
  23. JS::Object& ensure_web_namespace(DeprecatedString const& namespace_name)
  24. {
  25. if (auto it = m_namespaces.find(namespace_name); it != m_namespaces.end())
  26. return *it->value;
  27. create_web_namespace<NamespaceType>(*m_realm);
  28. return *m_namespaces.find(namespace_name)->value;
  29. }
  30. template<typename PrototypeType>
  31. JS::Object& ensure_web_prototype(DeprecatedString const& class_name)
  32. {
  33. if (auto it = m_prototypes.find(class_name); it != m_prototypes.end())
  34. return *it->value;
  35. create_web_prototype_and_constructor<PrototypeType>(*m_realm);
  36. return *m_prototypes.find(class_name)->value;
  37. }
  38. template<typename PrototypeType>
  39. JS::NativeFunction& ensure_web_constructor(DeprecatedString const& class_name)
  40. {
  41. if (auto it = m_constructors.find(class_name); it != m_constructors.end())
  42. return *it->value;
  43. create_web_prototype_and_constructor<PrototypeType>(*m_realm);
  44. return *m_constructors.find(class_name)->value;
  45. }
  46. private:
  47. virtual void visit_edges(JS::Cell::Visitor&) override;
  48. template<typename NamespaceType>
  49. void create_web_namespace(JS::Realm& realm);
  50. template<typename PrototypeType>
  51. void create_web_prototype_and_constructor(JS::Realm& realm);
  52. HashMap<DeprecatedString, JS::NonnullGCPtr<JS::Object>> m_namespaces;
  53. HashMap<DeprecatedString, JS::NonnullGCPtr<JS::Object>> m_prototypes;
  54. HashMap<DeprecatedString, JS::GCPtr<JS::NativeFunction>> m_constructors;
  55. JS::NonnullGCPtr<JS::Realm> m_realm;
  56. };
  57. [[nodiscard]] inline Intrinsics& host_defined_intrinsics(JS::Realm& realm)
  58. {
  59. return *verify_cast<HostDefined>(realm.host_defined())->intrinsics;
  60. }
  61. template<typename T>
  62. [[nodiscard]] JS::Object& ensure_web_namespace(JS::Realm& realm, DeprecatedString const& namespace_name)
  63. {
  64. return host_defined_intrinsics(realm).ensure_web_namespace<T>(namespace_name);
  65. }
  66. template<typename T>
  67. [[nodiscard]] JS::Object& ensure_web_prototype(JS::Realm& realm, DeprecatedString const& class_name)
  68. {
  69. return host_defined_intrinsics(realm).ensure_web_prototype<T>(class_name);
  70. }
  71. template<typename T>
  72. [[nodiscard]] JS::NativeFunction& ensure_web_constructor(JS::Realm& realm, DeprecatedString const& class_name)
  73. {
  74. return host_defined_intrinsics(realm).ensure_web_constructor<T>(class_name);
  75. }
  76. }