IteratorHelper.h 1.5 KB

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