2021-08-21 17:41:32 +00:00
|
|
|
/*
|
2022-01-31 18:07:22 +00:00
|
|
|
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
2021-08-21 17:41:32 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-03-17 00:26:49 +00:00
|
|
|
#include <AK/StringView.h>
|
2021-08-21 17:41:32 +00:00
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
|
|
|
#include <LibJS/Runtime/PromiseReaction.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
struct RemainingElements final : public Cell {
|
|
|
|
RemainingElements() = default;
|
|
|
|
|
|
|
|
explicit RemainingElements(u64 initial_value)
|
|
|
|
: value(initial_value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-17 00:26:49 +00:00
|
|
|
virtual StringView class_name() const override { return "RemainingElements"sv; }
|
2021-08-21 17:41:32 +00:00
|
|
|
|
|
|
|
u64 value { 0 };
|
|
|
|
};
|
|
|
|
|
2021-09-11 20:16:30 +00:00
|
|
|
class PromiseValueList final : public Cell {
|
|
|
|
public:
|
2021-08-21 17:41:32 +00:00
|
|
|
PromiseValueList()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-11 20:16:30 +00:00
|
|
|
Vector<Value>& values() { return m_values; }
|
|
|
|
Vector<Value> const& values() const { return m_values; }
|
|
|
|
|
|
|
|
private:
|
2022-03-17 00:26:49 +00:00
|
|
|
virtual StringView class_name() const override { return "PromiseValueList"sv; }
|
2022-02-04 15:22:47 +00:00
|
|
|
virtual void visit_edges(Visitor&) override;
|
2021-08-21 17:41:32 +00:00
|
|
|
|
2021-09-11 20:16:30 +00:00
|
|
|
Vector<Value> m_values;
|
2021-08-21 17:41:32 +00:00
|
|
|
};
|
|
|
|
|
2021-08-21 19:23:12 +00:00
|
|
|
class PromiseResolvingElementFunction : public NativeFunction {
|
2021-08-21 17:41:32 +00:00
|
|
|
JS_OBJECT(PromiseResolvingFunction, NativeFunction);
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void initialize(GlobalObject&) override;
|
2021-08-21 19:23:12 +00:00
|
|
|
virtual ~PromiseResolvingElementFunction() override = default;
|
2021-08-21 17:41:32 +00:00
|
|
|
|
2021-10-20 20:16:30 +00:00
|
|
|
virtual ThrowCompletionOr<Value> call() override;
|
2021-08-21 17:41:32 +00:00
|
|
|
|
2021-08-21 19:23:12 +00:00
|
|
|
protected:
|
|
|
|
explicit PromiseResolvingElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
|
|
|
|
2022-01-04 21:04:22 +00:00
|
|
|
virtual ThrowCompletionOr<Value> resolve_element() = 0;
|
2021-08-21 17:41:32 +00:00
|
|
|
|
|
|
|
size_t m_index { 0 };
|
|
|
|
PromiseValueList& m_values;
|
|
|
|
PromiseCapability m_capability;
|
|
|
|
RemainingElements& m_remaining_elements;
|
2021-08-21 19:23:12 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
2021-08-21 17:41:32 +00:00
|
|
|
bool m_already_called { false };
|
|
|
|
};
|
|
|
|
|
2021-08-21 19:23:12 +00:00
|
|
|
// 27.2.4.1.3 Promise.all Resolve Element Functions, https://tc39.es/ecma262/#sec-promise.all-resolve-element-functions
|
|
|
|
class PromiseAllResolveElementFunction final : public PromiseResolvingElementFunction {
|
|
|
|
JS_OBJECT(PromiseResolvingFunction, NativeFunction);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static PromiseAllResolveElementFunction* create(GlobalObject&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&);
|
|
|
|
|
|
|
|
explicit PromiseAllResolveElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
|
|
|
virtual ~PromiseAllResolveElementFunction() override = default;
|
|
|
|
|
|
|
|
private:
|
2022-01-04 21:04:22 +00:00
|
|
|
virtual ThrowCompletionOr<Value> resolve_element() override;
|
2021-08-21 19:23:12 +00:00
|
|
|
};
|
|
|
|
|
2021-08-21 21:00:48 +00:00
|
|
|
// 27.2.4.2.2 Promise.allSettled Resolve Element Functions, https://tc39.es/ecma262/#sec-promise.allsettled-resolve-element-functions
|
|
|
|
class PromiseAllSettledResolveElementFunction final : public PromiseResolvingElementFunction {
|
|
|
|
JS_OBJECT(PromiseResolvingFunction, NativeFunction);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static PromiseAllSettledResolveElementFunction* create(GlobalObject&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&);
|
|
|
|
|
|
|
|
explicit PromiseAllSettledResolveElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
|
|
|
virtual ~PromiseAllSettledResolveElementFunction() override = default;
|
|
|
|
|
|
|
|
private:
|
2022-01-04 21:04:22 +00:00
|
|
|
virtual ThrowCompletionOr<Value> resolve_element() override;
|
2021-08-21 21:00:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 27.2.4.2.3 Promise.allSettled Reject Element Functions, https://tc39.es/ecma262/#sec-promise.allsettled-reject-element-functions
|
|
|
|
class PromiseAllSettledRejectElementFunction final : public PromiseResolvingElementFunction {
|
2021-09-11 20:26:53 +00:00
|
|
|
JS_OBJECT(PromiseResolvingFunction, PromiseResolvingElementFunction);
|
2021-08-21 21:00:48 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static PromiseAllSettledRejectElementFunction* create(GlobalObject&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&);
|
|
|
|
|
|
|
|
explicit PromiseAllSettledRejectElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
|
|
|
virtual ~PromiseAllSettledRejectElementFunction() override = default;
|
|
|
|
|
|
|
|
private:
|
2022-01-04 21:04:22 +00:00
|
|
|
virtual ThrowCompletionOr<Value> resolve_element() override;
|
2021-08-21 21:00:48 +00:00
|
|
|
};
|
|
|
|
|
2021-08-21 20:43:38 +00:00
|
|
|
// 27.2.4.3.2 Promise.any Reject Element Functions, https://tc39.es/ecma262/#sec-promise.any-reject-element-functions
|
|
|
|
class PromiseAnyRejectElementFunction final : public PromiseResolvingElementFunction {
|
2021-09-11 20:26:53 +00:00
|
|
|
JS_OBJECT(PromiseResolvingFunction, PromiseResolvingElementFunction);
|
2021-08-21 20:43:38 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static PromiseAnyRejectElementFunction* create(GlobalObject&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&);
|
|
|
|
|
|
|
|
explicit PromiseAnyRejectElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
|
|
|
virtual ~PromiseAnyRejectElementFunction() override = default;
|
|
|
|
|
|
|
|
private:
|
2022-01-04 21:04:22 +00:00
|
|
|
virtual ThrowCompletionOr<Value> resolve_element() override;
|
2021-08-21 20:43:38 +00:00
|
|
|
};
|
|
|
|
|
2021-08-21 17:41:32 +00:00
|
|
|
}
|