2023-06-25 09:39:33 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Function.h>
|
2023-06-25 09:39:33 -04:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2023-07-16 15:04:59 -04:00
|
|
|
#include <LibJS/Runtime/GeneratorObject.h>
|
2023-06-25 09:39:33 -04:00
|
|
|
#include <LibJS/Runtime/Iterator.h>
|
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2025-07-19 10:41:08 -07:00
|
|
|
class IteratorHelper final : public GeneratorObject {
|
2023-07-16 15:04:59 -04:00
|
|
|
JS_OBJECT(IteratorHelper, GeneratorObject);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(IteratorHelper);
|
2023-06-25 09:39:33 -04:00
|
|
|
|
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
using Closure = GC::Function<ThrowCompletionOr<Value>(VM&, IteratorHelper&)>;
|
|
|
|
using AbruptClosure = GC::Function<ThrowCompletionOr<Value>(VM&, IteratorHelper&, Completion const&)>;
|
2023-06-25 09:39:33 -04:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
static ThrowCompletionOr<GC::Ref<IteratorHelper>> create(Realm&, GC::Ref<IteratorRecord>, GC::Ref<Closure>, GC::Ptr<AbruptClosure> = {});
|
2023-06-25 09:39:33 -04:00
|
|
|
|
2024-08-27 12:22:03 -04:00
|
|
|
IteratorRecord& underlying_iterator() { return m_underlying_iterator; }
|
2023-06-25 09:39:33 -04:00
|
|
|
IteratorRecord const& underlying_iterator() const { return m_underlying_iterator; }
|
|
|
|
|
|
|
|
size_t counter() const { return m_counter; }
|
|
|
|
void increment_counter() { ++m_counter; }
|
|
|
|
|
|
|
|
Value result(Value);
|
2023-07-16 15:04:59 -04:00
|
|
|
ThrowCompletionOr<Value> close_result(VM&, Completion);
|
2023-06-25 09:39:33 -04:00
|
|
|
|
|
|
|
private:
|
2024-11-15 04:01:23 +13:00
|
|
|
IteratorHelper(Realm&, Object& prototype, GC::Ref<IteratorRecord>, GC::Ref<Closure>, GC::Ptr<AbruptClosure>);
|
2023-06-25 09:39:33 -04:00
|
|
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
2025-05-09 04:23:30 +03:00
|
|
|
virtual ThrowCompletionOr<IterationResult> execute(VM&, JS::Completion const& completion) override;
|
2023-06-25 09:39:33 -04:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<IteratorRecord> m_underlying_iterator; // [[UnderlyingIterator]]
|
|
|
|
GC::Ref<Closure> m_closure;
|
|
|
|
GC::Ptr<AbruptClosure> m_abrupt_closure;
|
2023-06-25 09:39:33 -04:00
|
|
|
|
|
|
|
size_t m_counter { 0 };
|
|
|
|
bool m_done { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|