
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.
125 lines
4.4 KiB
C++
125 lines
4.4 KiB
C++
/*
|
|
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/StringView.h>
|
|
#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)
|
|
{
|
|
}
|
|
|
|
virtual StringView class_name() const override { return "RemainingElements"sv; }
|
|
|
|
u64 value { 0 };
|
|
};
|
|
|
|
class PromiseValueList final : public Cell {
|
|
public:
|
|
PromiseValueList()
|
|
{
|
|
}
|
|
|
|
Vector<Value>& values() { return m_values; }
|
|
Vector<Value> const& values() const { return m_values; }
|
|
|
|
private:
|
|
virtual StringView class_name() const override { return "PromiseValueList"sv; }
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
Vector<Value> m_values;
|
|
};
|
|
|
|
class PromiseResolvingElementFunction : public NativeFunction {
|
|
JS_OBJECT(PromiseResolvingFunction, NativeFunction);
|
|
|
|
public:
|
|
virtual void initialize(Realm&) override;
|
|
virtual ~PromiseResolvingElementFunction() override = default;
|
|
|
|
virtual ThrowCompletionOr<Value> call() override;
|
|
|
|
protected:
|
|
explicit PromiseResolvingElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
|
|
|
virtual ThrowCompletionOr<Value> resolve_element() = 0;
|
|
|
|
size_t m_index { 0 };
|
|
PromiseValueList& m_values;
|
|
PromiseCapability m_capability;
|
|
RemainingElements& m_remaining_elements;
|
|
|
|
private:
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
bool m_already_called { false };
|
|
};
|
|
|
|
// 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(Realm&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&);
|
|
|
|
explicit PromiseAllResolveElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
|
virtual ~PromiseAllResolveElementFunction() override = default;
|
|
|
|
private:
|
|
virtual ThrowCompletionOr<Value> resolve_element() override;
|
|
};
|
|
|
|
// 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(Realm&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&);
|
|
|
|
explicit PromiseAllSettledResolveElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
|
virtual ~PromiseAllSettledResolveElementFunction() override = default;
|
|
|
|
private:
|
|
virtual ThrowCompletionOr<Value> resolve_element() override;
|
|
};
|
|
|
|
// 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 {
|
|
JS_OBJECT(PromiseResolvingFunction, PromiseResolvingElementFunction);
|
|
|
|
public:
|
|
static PromiseAllSettledRejectElementFunction* create(Realm&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&);
|
|
|
|
explicit PromiseAllSettledRejectElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
|
virtual ~PromiseAllSettledRejectElementFunction() override = default;
|
|
|
|
private:
|
|
virtual ThrowCompletionOr<Value> resolve_element() override;
|
|
};
|
|
|
|
// 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 {
|
|
JS_OBJECT(PromiseResolvingFunction, PromiseResolvingElementFunction);
|
|
|
|
public:
|
|
static PromiseAnyRejectElementFunction* create(Realm&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&);
|
|
|
|
explicit PromiseAnyRejectElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
|
virtual ~PromiseAnyRejectElementFunction() override = default;
|
|
|
|
private:
|
|
virtual ThrowCompletionOr<Value> resolve_element() override;
|
|
};
|
|
|
|
}
|