| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-07-14 21:57:49 +01:00
										 |  |  |  * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Variant.h>
 | 
					
						
							| 
									
										
										
										
											2023-07-14 21:57:49 +01:00
										 |  |  | #include <LibJS/Bytecode/Interpreter.h>
 | 
					
						
							| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  | #include <LibJS/Runtime/ExecutionContext.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Object.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 27.6.2 Properties of AsyncGenerator Instances, https://tc39.es/ecma262/#sec-properties-of-asyncgenerator-intances
 | 
					
						
							| 
									
										
										
										
											2025-06-28 21:39:13 -07:00
										 |  |  | class JS_API AsyncGenerator final : public Object { | 
					
						
							| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  |     JS_OBJECT(AsyncGenerator, Object); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC_DECLARE_ALLOCATOR(AsyncGenerator); | 
					
						
							| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     enum class State { | 
					
						
							|  |  |  |         SuspendedStart, | 
					
						
							|  |  |  |         SuspendedYield, | 
					
						
							|  |  |  |         Executing, | 
					
						
							|  |  |  |         AwaitingReturn, | 
					
						
							|  |  |  |         Completed, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     static ThrowCompletionOr<GC::Ref<AsyncGenerator>> create(Realm&, Value, ECMAScriptFunctionObject*, NonnullOwnPtr<ExecutionContext>); | 
					
						
							| 
									
										
										
										
											2023-07-14 21:57:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-16 10:38:35 +02:00
										 |  |  |     virtual ~AsyncGenerator() override; | 
					
						
							| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     void async_generator_enqueue(Completion, GC::Ref<PromiseCapability>); | 
					
						
							| 
									
										
										
										
											2023-07-14 21:57:49 +01:00
										 |  |  |     ThrowCompletionOr<void> resume(VM&, Completion completion); | 
					
						
							| 
									
										
										
										
											2023-07-14 22:23:43 +01:00
										 |  |  |     void await_return(); | 
					
						
							| 
									
										
										
										
											2023-07-14 21:57:49 +01:00
										 |  |  |     void complete_step(Completion, bool done, Realm* realm = nullptr); | 
					
						
							|  |  |  |     void drain_queue(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     State async_generator_state() const { return m_async_generator_state; } | 
					
						
							|  |  |  |     void set_async_generator_state(Badge<AsyncGeneratorPrototype>, State value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Optional<String> const& generator_brand() const { return m_generator_brand; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-11-27 16:45:45 +01:00
										 |  |  |     AsyncGenerator(Realm&, Object& prototype, NonnullOwnPtr<ExecutionContext>); | 
					
						
							| 
									
										
										
										
											2022-08-28 23:51:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  |     virtual void visit_edges(Cell::Visitor&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 21:57:49 +01:00
										 |  |  |     void execute(VM&, Completion completion); | 
					
						
							|  |  |  |     ThrowCompletionOr<void> await(Value); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  |     // At the time of constructing an AsyncGenerator, we still need to point to an
 | 
					
						
							|  |  |  |     // execution context on the stack, but later need to 'adopt' it.
 | 
					
						
							| 
									
										
										
										
											2023-11-27 16:45:45 +01:00
										 |  |  |     State m_async_generator_state { State::SuspendedStart };   // [[AsyncGeneratorState]]
 | 
					
						
							|  |  |  |     NonnullOwnPtr<ExecutionContext> m_async_generator_context; // [[AsyncGeneratorContext]]
 | 
					
						
							|  |  |  |     Vector<AsyncGeneratorRequest> m_async_generator_queue;     // [[AsyncGeneratorQueue]]
 | 
					
						
							|  |  |  |     Optional<String> m_generator_brand;                        // [[GeneratorBrand]]
 | 
					
						
							| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<ECMAScriptFunctionObject> m_generating_function; | 
					
						
							| 
									
										
										
										
											2023-07-14 21:57:49 +01:00
										 |  |  |     Value m_previous_value; | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<Promise> m_current_promise; | 
					
						
							| 
									
										
										
										
											2022-05-05 08:47:36 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |