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