TemporaryExecutionContext.h 854 B

1234567891011121314151617181920212223242526272829303132
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGC/Ptr.h>
  8. #include <LibWeb/Forward.h>
  9. namespace Web::HTML {
  10. // When JS is run from outside the context of any user script, we currently do not have a running execution context.
  11. // This results in a crash when we access VM::running_execution_context(). This is a spec issue. Until it is resolved,
  12. // this is a workaround to temporarily push an execution context.
  13. class TemporaryExecutionContext {
  14. public:
  15. enum class CallbacksEnabled {
  16. No,
  17. Yes,
  18. };
  19. explicit TemporaryExecutionContext(JS::Realm&, CallbacksEnabled = CallbacksEnabled::No);
  20. ~TemporaryExecutionContext();
  21. private:
  22. GC::Ref<JS::Realm> m_realm;
  23. CallbacksEnabled m_callbacks_enabled { CallbacksEnabled::No };
  24. };
  25. }