mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +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
Notes:
sideshowbarker
2024-07-18 05:22:20 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/417523507ea Pull-request: https://github.com/SerenityOS/serenity/pull/9562 Reviewed-by: https://github.com/linusg ✅
4 changed files with 62 additions and 36 deletions
|
@ -94,11 +94,11 @@ set(SOURCES
|
|||
Runtime/OrdinaryFunctionObject.cpp
|
||||
Runtime/PrimitiveString.cpp
|
||||
Runtime/Promise.cpp
|
||||
Runtime/PromiseAllResolveElementFunction.cpp
|
||||
Runtime/PromiseConstructor.cpp
|
||||
Runtime/PromiseJobs.cpp
|
||||
Runtime/PromisePrototype.cpp
|
||||
Runtime/PromiseReaction.cpp
|
||||
Runtime/PromiseResolvingElementFunctions.cpp
|
||||
Runtime/PromiseResolvingFunction.cpp
|
||||
Runtime/PropertyDescriptor.cpp
|
||||
Runtime/ProxyConstructor.cpp
|
||||
|
|
|
@ -12,9 +12,9 @@
|
|||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/IteratorOperations.h>
|
||||
#include <LibJS/Runtime/Promise.h>
|
||||
#include <LibJS/Runtime/PromiseAllResolveElementFunction.h>
|
||||
#include <LibJS/Runtime/PromiseConstructor.h>
|
||||
#include <LibJS/Runtime/PromiseReaction.h>
|
||||
#include <LibJS/Runtime/PromiseResolvingElementFunctions.h>
|
||||
#include <LibJS/Runtime/TemporaryClearException.h>
|
||||
|
||||
namespace JS {
|
||||
|
|
|
@ -7,17 +7,12 @@
|
|||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/PromiseAllResolveElementFunction.h>
|
||||
#include <LibJS/Runtime/PromiseReaction.h>
|
||||
#include <LibJS/Runtime/PromiseResolvingElementFunctions.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
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::PromiseResolvingElementFunction(size_t index, PromiseValueList& values, PromiseCapability capability, RemainingElements& remaining_elements, Object& prototype)
|
||||
: NativeFunction(prototype)
|
||||
, m_index(index)
|
||||
, 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);
|
||||
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)
|
||||
return js_undefined();
|
||||
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);
|
||||
|
||||
if (--m_remaining_elements.value == 0) {
|
||||
|
@ -51,15 +72,4 @@ Value PromiseAllResolveElementFunction::call()
|
|||
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;
|
||||
};
|
||||
|
||||
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
|
||||
class PromiseAllResolveElementFunction final : public NativeFunction {
|
||||
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 void initialize(GlobalObject&) override;
|
||||
virtual ~PromiseAllResolveElementFunction() override = default;
|
||||
|
||||
virtual Value call() override;
|
||||
|
||||
private:
|
||||
virtual void visit_edges(Visitor&) override;
|
||||
|
||||
size_t m_index { 0 };
|
||||
PromiseValueList& m_values;
|
||||
PromiseCapability m_capability;
|
||||
RemainingElements& m_remaining_elements;
|
||||
bool m_already_called { false };
|
||||
virtual Value resolve_element() override;
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in a new issue