ladybird/Userland/Libraries/LibJS/Runtime/Promise.h
Ali Mohammad Pur 3b0bf05fa5 LibJS: Implement async functions as generator functions in BC mode
This applies a simple transformation, and adds a simple wrapper that
translates the generator interface to the async function interface.
2021-11-12 13:01:59 +00:00

63 lines
1.4 KiB
C++

/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Vector.h>
#include <LibJS/Runtime/Object.h>
namespace JS {
ThrowCompletionOr<Object*> promise_resolve(GlobalObject&, Object& constructor, Value);
class Promise : public Object {
JS_OBJECT(Promise, Object);
public:
enum class State {
Pending,
Fulfilled,
Rejected,
};
enum class RejectionOperation {
Reject,
Handle,
};
static Promise* create(GlobalObject&);
explicit Promise(Object& prototype);
virtual ~Promise() = default;
State state() const { return m_state; }
Value result() const { return m_result; }
struct ResolvingFunctions {
FunctionObject& resolve;
FunctionObject& reject;
};
ResolvingFunctions create_resolving_functions();
Value fulfill(Value value);
Value reject(Value reason);
Value perform_then(Value on_fulfilled, Value on_rejected, Optional<PromiseCapability> result_capability);
protected:
virtual void visit_edges(Visitor&) override;
private:
bool is_settled() const { return m_state == State::Fulfilled || m_state == State::Rejected; }
void trigger_reactions() const;
State m_state { State::Pending };
Value m_result;
Vector<PromiseReaction*> m_fulfill_reactions;
Vector<PromiseReaction*> m_reject_reactions;
bool m_is_handled { false };
};
}