mirror of
https://github.com/RGBCube/serenity
synced 2025-05-26 01:55:08 +00:00

This is a continuation of the previous two commits. As allocating a JS cell already primarily involves a realm instead of a global object, and we'll need to pass one to the allocate() function itself eventually (it's bridged via the global object right now), the create() functions need to receive a realm as well. The plan is for this to be the highest-level function that actually receives a realm and passes it around, AOs on an even higher level will use the "current realm" concept via VM::current_realm() as that's what the spec assumes; passing around realms (or global objects, for that matter) on higher AO levels is pointless and unlike for allocating individual objects, which may happen outside of regular JS execution, we don't need control over the specific realm that is being used there.
66 lines
1.8 KiB
C++
66 lines
1.8 KiB
C++
/*
|
|
* Copyright (c) 2021-2022, 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(Realm&);
|
|
|
|
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();
|
|
|
|
void fulfill(Value value);
|
|
void reject(Value reason);
|
|
Value perform_then(Value on_fulfilled, Value on_rejected, Optional<PromiseCapability> result_capability);
|
|
|
|
bool is_handled() const { return m_is_handled; }
|
|
|
|
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;
|
|
|
|
// 27.2.6 Properties of Promise Instances, https://tc39.es/ecma262/#sec-properties-of-promise-instances
|
|
State m_state { State::Pending }; // [[PromiseState]]
|
|
Value m_result; // [[PromiseResult]]
|
|
Vector<PromiseReaction*> m_fulfill_reactions; // [[PromiseFulfillReactions]]
|
|
Vector<PromiseReaction*> m_reject_reactions; // [[PromiseRejectReactions]]
|
|
bool m_is_handled { false }; // [[PromiseIsHandled]]
|
|
};
|
|
|
|
}
|