mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 16:07:35 +00:00
LibJS: Generalize PromiseAllResolveElementFunction common functionality
The element-resolving functions on the Promise constructor are all very similar. To prepare for more of these functions to be implemented, break out common parts into a base class.
This commit is contained in:
parent
40bc378d81
commit
417523507e
4 changed files with 62 additions and 36 deletions
|
@ -94,11 +94,11 @@ set(SOURCES
|
||||||
Runtime/OrdinaryFunctionObject.cpp
|
Runtime/OrdinaryFunctionObject.cpp
|
||||||
Runtime/PrimitiveString.cpp
|
Runtime/PrimitiveString.cpp
|
||||||
Runtime/Promise.cpp
|
Runtime/Promise.cpp
|
||||||
Runtime/PromiseAllResolveElementFunction.cpp
|
|
||||||
Runtime/PromiseConstructor.cpp
|
Runtime/PromiseConstructor.cpp
|
||||||
Runtime/PromiseJobs.cpp
|
Runtime/PromiseJobs.cpp
|
||||||
Runtime/PromisePrototype.cpp
|
Runtime/PromisePrototype.cpp
|
||||||
Runtime/PromiseReaction.cpp
|
Runtime/PromiseReaction.cpp
|
||||||
|
Runtime/PromiseResolvingElementFunctions.cpp
|
||||||
Runtime/PromiseResolvingFunction.cpp
|
Runtime/PromiseResolvingFunction.cpp
|
||||||
Runtime/PropertyDescriptor.cpp
|
Runtime/PropertyDescriptor.cpp
|
||||||
Runtime/ProxyConstructor.cpp
|
Runtime/ProxyConstructor.cpp
|
||||||
|
|
|
@ -12,9 +12,9 @@
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/IteratorOperations.h>
|
#include <LibJS/Runtime/IteratorOperations.h>
|
||||||
#include <LibJS/Runtime/Promise.h>
|
#include <LibJS/Runtime/Promise.h>
|
||||||
#include <LibJS/Runtime/PromiseAllResolveElementFunction.h>
|
|
||||||
#include <LibJS/Runtime/PromiseConstructor.h>
|
#include <LibJS/Runtime/PromiseConstructor.h>
|
||||||
#include <LibJS/Runtime/PromiseReaction.h>
|
#include <LibJS/Runtime/PromiseReaction.h>
|
||||||
|
#include <LibJS/Runtime/PromiseResolvingElementFunctions.h>
|
||||||
#include <LibJS/Runtime/TemporaryClearException.h>
|
#include <LibJS/Runtime/TemporaryClearException.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
|
@ -7,17 +7,12 @@
|
||||||
#include <LibJS/Runtime/AbstractOperations.h>
|
#include <LibJS/Runtime/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/Array.h>
|
#include <LibJS/Runtime/Array.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/PromiseAllResolveElementFunction.h>
|
|
||||||
#include <LibJS/Runtime/PromiseReaction.h>
|
#include <LibJS/Runtime/PromiseReaction.h>
|
||||||
|
#include <LibJS/Runtime/PromiseResolvingElementFunctions.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
PromiseAllResolveElementFunction* PromiseAllResolveElementFunction::create(GlobalObject& global_object, size_t index, PromiseValueList& values, PromiseCapability capability, RemainingElements& remaining_elements)
|
PromiseResolvingElementFunction::PromiseResolvingElementFunction(size_t index, PromiseValueList& values, PromiseCapability capability, RemainingElements& remaining_elements, Object& prototype)
|
||||||
{
|
|
||||||
return global_object.heap().allocate<PromiseAllResolveElementFunction>(global_object, index, values, capability, remaining_elements, *global_object.function_prototype());
|
|
||||||
}
|
|
||||||
|
|
||||||
PromiseAllResolveElementFunction::PromiseAllResolveElementFunction(size_t index, PromiseValueList& values, PromiseCapability capability, RemainingElements& remaining_elements, Object& prototype)
|
|
||||||
: NativeFunction(prototype)
|
: NativeFunction(prototype)
|
||||||
, m_index(index)
|
, m_index(index)
|
||||||
, m_values(values)
|
, m_values(values)
|
||||||
|
@ -26,21 +21,47 @@ PromiseAllResolveElementFunction::PromiseAllResolveElementFunction(size_t index,
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void PromiseAllResolveElementFunction::initialize(GlobalObject& global_object)
|
void PromiseResolvingElementFunction::initialize(GlobalObject& global_object)
|
||||||
{
|
{
|
||||||
Base::initialize(global_object);
|
Base::initialize(global_object);
|
||||||
define_direct_property(vm().names.length, Value(1), Attribute::Configurable);
|
define_direct_property(vm().names.length, Value(1), Attribute::Configurable);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value PromiseAllResolveElementFunction::call()
|
Value PromiseResolvingElementFunction::call()
|
||||||
{
|
{
|
||||||
auto& vm = this->vm();
|
|
||||||
auto& global_object = this->global_object();
|
|
||||||
|
|
||||||
if (m_already_called)
|
if (m_already_called)
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
m_already_called = true;
|
m_already_called = true;
|
||||||
|
|
||||||
|
return resolve_element();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PromiseResolvingElementFunction::visit_edges(Cell::Visitor& visitor)
|
||||||
|
{
|
||||||
|
Base::visit_edges(visitor);
|
||||||
|
|
||||||
|
visitor.visit(&m_values);
|
||||||
|
visitor.visit(m_capability.promise);
|
||||||
|
visitor.visit(m_capability.resolve);
|
||||||
|
visitor.visit(m_capability.reject);
|
||||||
|
visitor.visit(&m_remaining_elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
PromiseAllResolveElementFunction* PromiseAllResolveElementFunction::create(GlobalObject& global_object, size_t index, PromiseValueList& values, PromiseCapability capability, RemainingElements& remaining_elements)
|
||||||
|
{
|
||||||
|
return global_object.heap().allocate<PromiseAllResolveElementFunction>(global_object, index, values, capability, remaining_elements, *global_object.function_prototype());
|
||||||
|
}
|
||||||
|
|
||||||
|
PromiseAllResolveElementFunction::PromiseAllResolveElementFunction(size_t index, PromiseValueList& values, PromiseCapability capability, RemainingElements& remaining_elements, Object& prototype)
|
||||||
|
: PromiseResolvingElementFunction(index, values, move(capability), remaining_elements, prototype)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Value PromiseAllResolveElementFunction::resolve_element()
|
||||||
|
{
|
||||||
|
auto& vm = this->vm();
|
||||||
|
auto& global_object = this->global_object();
|
||||||
|
|
||||||
m_values.values[m_index] = vm.argument(0);
|
m_values.values[m_index] = vm.argument(0);
|
||||||
|
|
||||||
if (--m_remaining_elements.value == 0) {
|
if (--m_remaining_elements.value == 0) {
|
||||||
|
@ -51,15 +72,4 @@ Value PromiseAllResolveElementFunction::call()
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PromiseAllResolveElementFunction::visit_edges(Cell::Visitor& visitor)
|
|
||||||
{
|
|
||||||
Base::visit_edges(visitor);
|
|
||||||
|
|
||||||
visitor.visit(&m_values);
|
|
||||||
visitor.visit(m_capability.promise);
|
|
||||||
visitor.visit(m_capability.resolve);
|
|
||||||
visitor.visit(m_capability.reject);
|
|
||||||
visitor.visit(&m_remaining_elements);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -35,27 +35,43 @@ struct PromiseValueList final : public Cell {
|
||||||
MarkedValueList values;
|
MarkedValueList values;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class PromiseResolvingElementFunction : public NativeFunction {
|
||||||
|
JS_OBJECT(PromiseResolvingFunction, NativeFunction);
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual void initialize(GlobalObject&) override;
|
||||||
|
virtual ~PromiseResolvingElementFunction() override = default;
|
||||||
|
|
||||||
|
virtual Value call() override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
explicit PromiseResolvingElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
||||||
|
|
||||||
|
virtual 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
|
// 27.2.4.1.3 Promise.all Resolve Element Functions, https://tc39.es/ecma262/#sec-promise.all-resolve-element-functions
|
||||||
class PromiseAllResolveElementFunction final : public NativeFunction {
|
class PromiseAllResolveElementFunction final : public PromiseResolvingElementFunction {
|
||||||
JS_OBJECT(PromiseResolvingFunction, NativeFunction);
|
JS_OBJECT(PromiseResolvingFunction, NativeFunction);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static PromiseAllResolveElementFunction* create(GlobalObject&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&);
|
static PromiseAllResolveElementFunction* create(GlobalObject&, size_t, PromiseValueList&, PromiseCapability, RemainingElements&);
|
||||||
|
|
||||||
explicit PromiseAllResolveElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
explicit PromiseAllResolveElementFunction(size_t, PromiseValueList&, PromiseCapability, RemainingElements&, Object& prototype);
|
||||||
virtual void initialize(GlobalObject&) override;
|
|
||||||
virtual ~PromiseAllResolveElementFunction() override = default;
|
virtual ~PromiseAllResolveElementFunction() override = default;
|
||||||
|
|
||||||
virtual Value call() override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void visit_edges(Visitor&) override;
|
virtual Value resolve_element() override;
|
||||||
|
|
||||||
size_t m_index { 0 };
|
|
||||||
PromiseValueList& m_values;
|
|
||||||
PromiseCapability m_capability;
|
|
||||||
RemainingElements& m_remaining_elements;
|
|
||||||
bool m_already_called { false };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue