1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:17:35 +00:00

LibJS+LibWeb: Spin event loop via VM::CustomData abstraction

Instead of calling Core::EventLoop directly, LibJS now has a virtual
function on VM::CustomData for customizing this behavior.

We use this in LibWeb to plumb the spin request through to the
PlatformEventPlugin.
This commit is contained in:
Andreas Kling 2022-09-08 00:13:39 +02:00
parent 9567e211e7
commit 7b0dd98103
4 changed files with 15 additions and 2 deletions

View file

@ -97,8 +97,11 @@ ThrowCompletionOr<Value> await(VM& vm, Value value)
// by letting the event loop spin until our promise is no longer pending, and then synchronously // by letting the event loop spin until our promise is no longer pending, and then synchronously
// running all queued promise jobs. // running all queued promise jobs.
// Note: This is not used by LibJS itself, and is performed for the embedder (i.e. LibWeb). // Note: This is not used by LibJS itself, and is performed for the embedder (i.e. LibWeb).
if (Core::EventLoop::has_been_instantiated()) if (auto* custom_data = vm.custom_data()) {
Core::EventLoop::current().spin_until([&] { return success.has_value(); }); custom_data->spin_event_loop_until([&] {
return success.has_value();
});
}
// 8. Remove asyncContext from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context. // 8. Remove asyncContext from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
// NOTE: Since we don't push any EC, this step is not performed. // NOTE: Since we don't push any EC, this step is not performed.

View file

@ -34,6 +34,8 @@ class VM : public RefCounted<VM> {
public: public:
struct CustomData { struct CustomData {
virtual ~CustomData() = default; virtual ~CustomData() = default;
virtual void spin_event_loop_until(Function<bool()> goal_condition) = 0;
}; };
static NonnullRefPtr<VM> create(OwnPtr<CustomData> = {}); static NonnullRefPtr<VM> create(OwnPtr<CustomData> = {});

View file

@ -21,6 +21,7 @@
#include <LibWeb/HTML/Scripting/Environments.h> #include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h> #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/HTML/Window.h> #include <LibWeb/HTML/Window.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
namespace Web::Bindings { namespace Web::Bindings {
@ -413,4 +414,9 @@ NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM& vm, Fu
return realm_execution_context; return realm_execution_context;
} }
void WebEngineCustomData::spin_event_loop_until(Function<bool()> goal_condition)
{
Platform::EventLoopPlugin::the().spin_until(move(goal_condition));
}
} }

View file

@ -20,6 +20,8 @@ namespace Web::Bindings {
struct WebEngineCustomData final : public JS::VM::CustomData { struct WebEngineCustomData final : public JS::VM::CustomData {
virtual ~WebEngineCustomData() override = default; virtual ~WebEngineCustomData() override = default;
virtual void spin_event_loop_until(Function<bool()> goal_condition) override;
HTML::EventLoop event_loop; HTML::EventLoop event_loop;
// FIXME: These should only be on similar-origin window agents, but we don't currently differentiate agent types. // FIXME: These should only be on similar-origin window agents, but we don't currently differentiate agent types.