IteratorHelper.h 1.6 KB

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