1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:57:44 +00:00

LibJS: Setup host hooks and have promise jobs work out the realm

This allows the host of LibJS (notably LibWeb in this case) to override
certain functions such as HostEnqueuePromiseJob, so it can do it's own
thing in certain situations. Notably, LibWeb will override
HostEnqueuePromiseJob to put promise jobs on the microtask queue.

This also makes promise jobs use AK::Function instead of
JS::NativeFunction. This removes the need to go through a JavaScript
function and it more closely matches the spec's idea of "abstract
closures"
This commit is contained in:
Luke Wilde 2022-02-06 03:46:45 +00:00 committed by Linus Groh
parent 5aacec65ab
commit 4c1c6ef91c
12 changed files with 202 additions and 172 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -13,41 +14,13 @@
namespace JS {
class PromiseReactionJob final : public NativeFunction {
JS_OBJECT(PromiseReactionJob, NativeFunction);
public:
static PromiseReactionJob* create(GlobalObject&, PromiseReaction&, Value argument);
explicit PromiseReactionJob(PromiseReaction&, Value argument, Object& prototype);
virtual ~PromiseReactionJob() override = default;
virtual ThrowCompletionOr<Value> call() override;
private:
virtual void visit_edges(Visitor&) override;
PromiseReaction& m_reaction;
Value m_argument;
struct PromiseJob {
Function<ThrowCompletionOr<Value>()> job;
Realm* realm { nullptr };
};
class PromiseResolveThenableJob final : public NativeFunction {
JS_OBJECT(PromiseReactionJob, NativeFunction);
public:
static PromiseResolveThenableJob* create(GlobalObject&, Promise&, Value thenable, JobCallback then);
explicit PromiseResolveThenableJob(Promise&, Value thenable, JobCallback then, Object& prototype);
virtual ~PromiseResolveThenableJob() override = default;
virtual ThrowCompletionOr<Value> call() override;
private:
virtual void visit_edges(Visitor&) override;
Promise& m_promise_to_resolve;
Value m_thenable;
JobCallback m_then;
};
// NOTE: These return a PromiseJob to prevent awkward casting at call sites.
PromiseJob create_promise_reaction_job(GlobalObject&, PromiseReaction&, Value argument);
PromiseJob create_promise_resolve_thenable_job(GlobalObject&, Promise&, Value thenable, JobCallback then);
}