MainThreadVM.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibJS/Forward.h>
  9. #include <LibJS/Runtime/JobCallback.h>
  10. #include <LibJS/Runtime/VM.h>
  11. #include <LibWeb/DOM/Element.h>
  12. #include <LibWeb/DOM/MutationObserver.h>
  13. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  14. namespace Web::Bindings {
  15. // https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-reactions-stack
  16. struct CustomElementReactionsStack {
  17. CustomElementReactionsStack() = default;
  18. ~CustomElementReactionsStack() = default;
  19. // https://html.spec.whatwg.org/multipage/custom-elements.html#element-queue
  20. // Each item in the stack is an element queue, which is initially empty as well. Each item in an element queue is an element.
  21. // (The elements are not necessarily custom yet, since this queue is used for upgrades as well.)
  22. Vector<Vector<JS::Handle<DOM::Element>>> element_queue_stack;
  23. // https://html.spec.whatwg.org/multipage/custom-elements.html#backup-element-queue
  24. // Each custom element reactions stack has an associated backup element queue, which an initially-empty element queue.
  25. Vector<JS::Handle<DOM::Element>> backup_element_queue;
  26. // https://html.spec.whatwg.org/multipage/custom-elements.html#processing-the-backup-element-queue
  27. // To prevent reentrancy when processing the backup element queue, each custom element reactions stack also has a processing the backup element queue flag, initially unset.
  28. bool processing_the_backup_element_queue { false };
  29. };
  30. struct WebEngineCustomData final : public JS::VM::CustomData {
  31. virtual ~WebEngineCustomData() override = default;
  32. virtual void spin_event_loop_until(JS::SafeFunction<bool()> goal_condition) override;
  33. HTML::EventLoop event_loop;
  34. // FIXME: These should only be on similar-origin window agents, but we don't currently differentiate agent types.
  35. // https://dom.spec.whatwg.org/#mutation-observer-compound-microtask-queued-flag
  36. bool mutation_observer_microtask_queued { false };
  37. // https://dom.spec.whatwg.org/#mutation-observer-list
  38. // FIXME: This should be a set.
  39. Vector<JS::NonnullGCPtr<DOM::MutationObserver>> mutation_observers;
  40. JS::Handle<JS::Realm> internal_realm;
  41. OwnPtr<JS::ExecutionContext> root_execution_context;
  42. // https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-reactions-stack
  43. // Each similar-origin window agent has a custom element reactions stack, which is initially empty.
  44. CustomElementReactionsStack custom_element_reactions_stack {};
  45. // https://html.spec.whatwg.org/multipage/custom-elements.html#current-element-queue
  46. // A similar-origin window agent's current element queue is the element queue at the top of its custom element reactions stack.
  47. Vector<JS::Handle<DOM::Element>>& current_element_queue() { return custom_element_reactions_stack.element_queue_stack.last(); }
  48. Vector<JS::Handle<DOM::Element>> const& current_element_queue() const { return custom_element_reactions_stack.element_queue_stack.last(); }
  49. };
  50. struct WebEngineCustomJobCallbackData final : public JS::JobCallback::CustomData {
  51. WebEngineCustomJobCallbackData(HTML::EnvironmentSettingsObject& incumbent_settings, OwnPtr<JS::ExecutionContext> active_script_context)
  52. : incumbent_settings(incumbent_settings)
  53. , active_script_context(move(active_script_context))
  54. {
  55. }
  56. virtual ~WebEngineCustomJobCallbackData() override = default;
  57. JS::NonnullGCPtr<HTML::EnvironmentSettingsObject> incumbent_settings;
  58. OwnPtr<JS::ExecutionContext> active_script_context;
  59. };
  60. HTML::Script* active_script();
  61. ErrorOr<void> initialize_main_thread_vm();
  62. JS::VM& main_thread_vm();
  63. void queue_mutation_observer_microtask(DOM::Document const&);
  64. NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM&, Function<JS::Object*(JS::Realm&)> create_global_object, Function<JS::Object*(JS::Realm&)> create_global_this_value);
  65. void invoke_custom_element_reactions(Vector<JS::Handle<DOM::Element>>& element_queue);
  66. }