GeneratorObject.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. public:
  14. static ThrowCompletionOr<NonnullGCPtr<GeneratorObject>> create(Realm&, Value, ECMAScriptFunctionObject*, ExecutionContext, Bytecode::CallFrame);
  15. virtual ~GeneratorObject() override = default;
  16. void visit_edges(Cell::Visitor&) override;
  17. ThrowCompletionOr<Value> resume(VM&, Value value, Optional<StringView> const& generator_brand);
  18. ThrowCompletionOr<Value> resume_abrupt(VM&, JS::Completion abrupt_completion, Optional<StringView> const& generator_brand);
  19. enum class GeneratorState {
  20. SuspendedStart,
  21. SuspendedYield,
  22. Executing,
  23. Completed,
  24. };
  25. GeneratorState generator_state() const { return m_generator_state; }
  26. void set_generator_state(GeneratorState generator_state) { m_generator_state = generator_state; }
  27. protected:
  28. GeneratorObject(Realm&, Object& prototype, ExecutionContext, Optional<StringView> generator_brand = {});
  29. ThrowCompletionOr<GeneratorState> validate(VM&, Optional<StringView> const& generator_brand);
  30. virtual ThrowCompletionOr<Value> execute(VM&, JS::Completion const& completion);
  31. private:
  32. ExecutionContext m_execution_context;
  33. GCPtr<ECMAScriptFunctionObject> m_generating_function;
  34. Value m_previous_value;
  35. Optional<Bytecode::CallFrame> m_frame;
  36. GeneratorState m_generator_state { GeneratorState::SuspendedStart };
  37. Optional<StringView> m_generator_brand;
  38. };
  39. }