IteratorHelper.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/Completion.h>
  8. #include <LibJS/Runtime/GeneratorObject.h>
  9. #include <LibJS/Runtime/Iterator.h>
  10. #include <LibJS/Runtime/Object.h>
  11. #include <LibJS/SafeFunction.h>
  12. namespace JS {
  13. class IteratorHelper final : public GeneratorObject {
  14. JS_OBJECT(IteratorHelper, GeneratorObject);
  15. public:
  16. using Closure = JS::SafeFunction<ThrowCompletionOr<Value>(VM&, IteratorHelper&)>;
  17. using AbruptClosure = JS::SafeFunction<ThrowCompletionOr<Value>(VM&, IteratorHelper&, Completion const&)>;
  18. static ThrowCompletionOr<NonnullGCPtr<IteratorHelper>> create(Realm&, IteratorRecord, Closure, Optional<AbruptClosure> = {});
  19. IteratorRecord const& underlying_iterator() const { return m_underlying_iterator; }
  20. size_t counter() const { return m_counter; }
  21. void increment_counter() { ++m_counter; }
  22. Value result(Value);
  23. ThrowCompletionOr<Value> close_result(VM&, Completion);
  24. private:
  25. IteratorHelper(Realm&, Object& prototype, IteratorRecord, Closure, Optional<AbruptClosure>);
  26. virtual void visit_edges(Visitor&) override;
  27. virtual ThrowCompletionOr<Value> execute(VM&, JS::Completion const& completion) override;
  28. IteratorRecord m_underlying_iterator; // [[UnderlyingIterator]]
  29. Closure m_closure;
  30. Optional<AbruptClosure> m_abrupt_closure;
  31. size_t m_counter { 0 };
  32. bool m_done { false };
  33. };
  34. }