diff --git a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp index 44edb408d0..cfdf20bf4a 100644 --- a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp +++ b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp @@ -13,7 +13,7 @@ JS::VM& main_thread_vm() { static RefPtr vm; if (!vm) - vm = JS::VM::create(); + vm = JS::VM::create(make()); return *vm; } diff --git a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.h b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.h index facabec2f3..5b40724840 100644 --- a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.h +++ b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.h @@ -7,9 +7,16 @@ #pragma once #include +#include namespace Web::Bindings { +struct WebEngineCustomData final : public JS::VM::CustomData { + virtual ~WebEngineCustomData() override { } + + HTML::EventLoop event_loop; +}; + JS::VM& main_thread_vm(); } diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 288c0538b3..c04837f9b1 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -71,6 +71,9 @@ set(SOURCES HTML/BrowsingContextContainer.cpp HTML/CanvasRenderingContext2D.cpp HTML/DOMParser.cpp + HTML/EventLoop/EventLoop.cpp + HTML/EventLoop/Task.cpp + HTML/EventLoop/TaskQueue.cpp HTML/EventNames.cpp HTML/FormAssociatedElement.cpp HTML/GlobalEventHandlers.cpp diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp new file mode 100644 index 0000000000..8c89652543 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace Web::HTML { + +EventLoop::EventLoop() +{ +} + +EventLoop::~EventLoop() +{ +} + +EventLoop& main_thread_event_loop() +{ + return static_cast(Bindings::main_thread_vm().custom_data())->event_loop; +} + +} diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h new file mode 100644 index 0000000000..5f1ecf878e --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::HTML { + +class EventLoop { +public: + EventLoop(); + ~EventLoop(); + + TaskQueue& task_queue() { return m_task_queue; } + TaskQueue const& task_queue() const { return m_task_queue; } + +private: + TaskQueue m_task_queue; +}; + +EventLoop& main_thread_event_loop(); + +} diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/Task.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/Task.cpp new file mode 100644 index 0000000000..a913fcb815 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/Task.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace Web::HTML { + +Task::Task(DOM::Document* document, Function steps) + : m_steps(move(steps)) + , m_document(document) +{ +} + +Task::~Task() +{ +} + +void Task::execute() +{ + m_steps(); +} + +} diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/Task.h b/Userland/Libraries/LibWeb/HTML/EventLoop/Task.h new file mode 100644 index 0000000000..d84ec08db6 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/Task.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Web::HTML { + +class Task { +public: + static NonnullOwnPtr create(DOM::Document* document, Function steps) + { + return adopt_own(*new Task(document, move(steps))); + } + ~Task(); + + void execute(); + + DOM::Document* document() { return m_document; } + DOM::Document const* document() const { return m_document; } + +private: + Task(DOM::Document*, Function steps); + + Function m_steps; + RefPtr m_document; +}; + +} diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp new file mode 100644 index 0000000000..e076a97431 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace Web::HTML { + +TaskQueue::TaskQueue() +{ +} + +TaskQueue::~TaskQueue() +{ +} + +void TaskQueue::add(NonnullOwnPtr task) +{ + m_tasks.enqueue(move(task)); +} + +} diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.h b/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.h new file mode 100644 index 0000000000..0bc8e11eb5 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::HTML { + +class TaskQueue { +public: + TaskQueue(); + ~TaskQueue(); + + bool is_empty() const { return m_tasks.is_empty(); } + + void add(NonnullOwnPtr); + OwnPtr take_first_runnable() { return m_tasks.dequeue(); } + +private: + Queue> m_tasks; +}; + +}