GeneratorObject.h 1.6 KB

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