IteratorHelper.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. static ThrowCompletionOr<NonnullGCPtr<IteratorHelper>> create(Realm&, IteratorRecord, Closure);
  18. IteratorRecord const& underlying_iterator() const { return m_underlying_iterator; }
  19. size_t counter() const { return m_counter; }
  20. void increment_counter() { ++m_counter; }
  21. Value result(Value);
  22. ThrowCompletionOr<Value> close_result(VM&, Completion);
  23. private:
  24. IteratorHelper(Realm&, Object& prototype, IteratorRecord, Closure);
  25. virtual void visit_edges(Visitor&) override;
  26. virtual ThrowCompletionOr<Value> execute(VM&, JS::Completion const& completion) override;
  27. IteratorRecord m_underlying_iterator; // [[UnderlyingIterator]]
  28. Closure m_closure;
  29. size_t m_counter { 0 };
  30. bool m_done { false };
  31. };
  32. }