GeneratorObject.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Bytecode/Interpreter.h>
  8. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  9. #include <LibJS/Runtime/Object.h>
  10. namespace JS {
  11. class GeneratorObject : public Object {
  12. JS_OBJECT(GeneratorObject, Object);
  13. JS_DECLARE_ALLOCATOR(GeneratorObject);
  14. public:
  15. static ThrowCompletionOr<NonnullGCPtr<GeneratorObject>> create(Realm&, Value, ECMAScriptFunctionObject*, NonnullOwnPtr<ExecutionContext>, NonnullOwnPtr<Bytecode::CallFrame>);
  16. virtual ~GeneratorObject() override = default;
  17. void visit_edges(Cell::Visitor&) override;
  18. ThrowCompletionOr<Value> resume(VM&, Value value, Optional<StringView> const& generator_brand);
  19. ThrowCompletionOr<Value> resume_abrupt(VM&, JS::Completion abrupt_completion, Optional<StringView> const& generator_brand);
  20. enum class GeneratorState {
  21. SuspendedStart,
  22. SuspendedYield,
  23. Executing,
  24. Completed,
  25. };
  26. GeneratorState generator_state() const { return m_generator_state; }
  27. void set_generator_state(GeneratorState generator_state) { m_generator_state = generator_state; }
  28. protected:
  29. GeneratorObject(Realm&, Object& prototype, NonnullOwnPtr<ExecutionContext>, Optional<StringView> generator_brand = {});
  30. ThrowCompletionOr<GeneratorState> validate(VM&, Optional<StringView> const& generator_brand);
  31. virtual ThrowCompletionOr<Value> execute(VM&, JS::Completion const& completion);
  32. private:
  33. NonnullOwnPtr<ExecutionContext> m_execution_context;
  34. GCPtr<ECMAScriptFunctionObject> m_generating_function;
  35. Value m_previous_value;
  36. OwnPtr<Bytecode::CallFrame> m_frame;
  37. GeneratorState m_generator_state { GeneratorState::SuspendedStart };
  38. Optional<StringView> m_generator_brand;
  39. };
  40. }