1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:57:45 +00:00

LibJS: Spin the event loop until await has completed

Before this the event loop was spun until the state of the promise was
not pending, however it is possible that a promise has already been
fulfilled/rejected when awaiting it. This could then lead to a crash
below as it would not pump the event loop in such cases.
Although this change is in LibJS, it really only impacts any usage of
LibJS within a EventLoop environment such as LibWeb.

Instead of checking the state of the promise we know check that success
has a value which can only happen if either the fulfilled or rejected
closure set up by await are called.
This commit is contained in:
davidot 2022-02-28 23:43:07 +01:00 committed by Linus Groh
parent 4170fcb777
commit 244adb371f

View file

@ -98,7 +98,7 @@ ThrowCompletionOr<Value> await(GlobalObject& global_object, Value value)
// running all queued promise jobs.
// Note: This is not used by LibJS itself, and is performed for the embedder (i.e. LibWeb).
if (Core::EventLoop::has_been_instantiated())
Core::EventLoop::current().spin_until([&] { return promise->state() != Promise::State::Pending; });
Core::EventLoop::current().spin_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.
// NOTE: Since we don't push any EC, this step is not performed.