2021-06-10 21:08:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-09-11 20:23:42 +00:00
|
|
|
#include <LibJS/Bytecode/Interpreter.h>
|
2021-09-24 20:40:38 +00:00
|
|
|
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>
|
2021-06-10 21:08:30 +00:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2023-07-16 18:37:35 +00:00
|
|
|
class GeneratorObject : public Object {
|
2021-06-10 21:08:30 +00:00
|
|
|
JS_OBJECT(GeneratorObject, Object);
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DECLARE_ALLOCATOR(GeneratorObject);
|
2021-06-10 21:08:30 +00:00
|
|
|
|
|
|
|
public:
|
2024-11-14 15:01:23 +00:00
|
|
|
static ThrowCompletionOr<GC::Ref<GeneratorObject>> create(Realm&, Value, ECMAScriptFunctionObject*, NonnullOwnPtr<ExecutionContext>);
|
2022-03-14 16:25:06 +00:00
|
|
|
virtual ~GeneratorObject() override = default;
|
2021-06-10 21:08:30 +00:00
|
|
|
void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
2023-07-16 18:33:20 +00:00
|
|
|
ThrowCompletionOr<Value> resume(VM&, Value value, Optional<StringView> const& generator_brand);
|
|
|
|
ThrowCompletionOr<Value> resume_abrupt(VM&, JS::Completion abrupt_completion, Optional<StringView> const& generator_brand);
|
2021-06-10 21:08:30 +00:00
|
|
|
|
2022-11-25 23:14:08 +00:00
|
|
|
enum class GeneratorState {
|
|
|
|
SuspendedStart,
|
|
|
|
SuspendedYield,
|
|
|
|
Executing,
|
|
|
|
Completed,
|
|
|
|
};
|
2023-07-16 18:37:35 +00:00
|
|
|
GeneratorState generator_state() const { return m_generator_state; }
|
|
|
|
void set_generator_state(GeneratorState generator_state) { m_generator_state = generator_state; }
|
|
|
|
|
|
|
|
protected:
|
2023-11-27 15:45:45 +00:00
|
|
|
GeneratorObject(Realm&, Object& prototype, NonnullOwnPtr<ExecutionContext>, Optional<StringView> generator_brand = {});
|
2022-11-25 23:14:08 +00:00
|
|
|
|
2023-07-16 18:33:20 +00:00
|
|
|
ThrowCompletionOr<GeneratorState> validate(VM&, Optional<StringView> const& generator_brand);
|
2023-07-16 18:37:35 +00:00
|
|
|
virtual ThrowCompletionOr<Value> execute(VM&, JS::Completion const& completion);
|
2022-11-25 23:14:08 +00:00
|
|
|
|
2023-07-16 18:37:35 +00:00
|
|
|
private:
|
2023-11-27 15:45:45 +00:00
|
|
|
NonnullOwnPtr<ExecutionContext> m_execution_context;
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ptr<ECMAScriptFunctionObject> m_generating_function;
|
2021-06-10 21:08:30 +00:00
|
|
|
Value m_previous_value;
|
2022-11-25 23:14:08 +00:00
|
|
|
GeneratorState m_generator_state { GeneratorState::SuspendedStart };
|
2023-07-16 18:33:20 +00:00
|
|
|
Optional<StringView> m_generator_brand;
|
2021-06-10 21:08:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|