Serenity Operating System
1/*
2 * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/StringView.h>
10#include <LibJS/Runtime/NativeFunction.h>
11
12namespace JS {
13
14struct AlreadyResolved final : public Cell {
15 JS_CELL(AlreadyResolved, Cell);
16
17 bool value { false };
18
19protected:
20 // Allocated cells must be >= sizeof(FreelistEntry), which is 24 bytes -
21 // but AlreadyResolved is only 16 bytes without this.
22 u8 dummy[8];
23};
24
25class PromiseResolvingFunction final : public NativeFunction {
26 JS_OBJECT(PromiseResolvingFunction, NativeFunction);
27
28public:
29 using FunctionType = Function<ThrowCompletionOr<Value>(VM&, Promise&, AlreadyResolved&)>;
30
31 static NonnullGCPtr<PromiseResolvingFunction> create(Realm&, Promise&, AlreadyResolved&, FunctionType);
32
33 virtual ThrowCompletionOr<void> initialize(Realm&) override;
34 virtual ~PromiseResolvingFunction() override = default;
35
36 virtual ThrowCompletionOr<Value> call() override;
37
38private:
39 explicit PromiseResolvingFunction(Promise&, AlreadyResolved&, FunctionType, Object& prototype);
40
41 virtual void visit_edges(Visitor&) override;
42
43 Promise& m_promise;
44 AlreadyResolved& m_already_resolved;
45 FunctionType m_native_function;
46};
47
48}