14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_JS_ASYNC_GENERATOR_OBJECT_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_JS_ASYNC_GENERATOR_OBJECT_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/js_object-inl.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h"
224514f5e3Sopenharmony_ci#include "ecmascript/record.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_function.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_generator_object.h"
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cinamespace panda {
274514f5e3Sopenharmony_cinamespace ecmascript {
284514f5e3Sopenharmony_cienum class JSAsyncGeneratorState : uint8_t {
294514f5e3Sopenharmony_ci    UNDEFINED = 0,
304514f5e3Sopenharmony_ci    SUSPENDED_START,
314514f5e3Sopenharmony_ci    SUSPENDED_YIELD,
324514f5e3Sopenharmony_ci    EXECUTING,
334514f5e3Sopenharmony_ci    COMPLETED,
344514f5e3Sopenharmony_ci    AWAITING_RETURN,
354514f5e3Sopenharmony_ci};
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_cienum class AsyncGeneratorResumeMode : uint8_t {
384514f5e3Sopenharmony_ci    RETURN = 0,
394514f5e3Sopenharmony_ci    THROW,
404514f5e3Sopenharmony_ci    NEXT,
414514f5e3Sopenharmony_ci    UNDEFINED
424514f5e3Sopenharmony_ci};
434514f5e3Sopenharmony_ci
444514f5e3Sopenharmony_ciclass AsyncGeneratorRequest final : public Record {
454514f5e3Sopenharmony_cipublic:
464514f5e3Sopenharmony_ci    CAST_CHECK(AsyncGeneratorRequest, IsAsyncGeneratorRequest);
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci    static constexpr size_t COMPLETION_OFFSET = Record::SIZE;
494514f5e3Sopenharmony_ci    ACCESSORS(Completion, COMPLETION_OFFSET, CAPABILITY_OFFSET);
504514f5e3Sopenharmony_ci    ACCESSORS(Capability, CAPABILITY_OFFSET, SIZE);
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    DECL_DUMP()
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT(COMPLETION_OFFSET, SIZE)
554514f5e3Sopenharmony_ci};
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ciclass JSAsyncGeneratorObject : public JSObject {
584514f5e3Sopenharmony_cipublic:
594514f5e3Sopenharmony_ci    CAST_CHECK(JSAsyncGeneratorObject, IsAsyncGeneratorObject);
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci    static constexpr size_t GENERATOR_CONTEXT_OFFSET = JSObject::SIZE;
624514f5e3Sopenharmony_ci    ACCESSORS(GeneratorContext, GENERATOR_CONTEXT_OFFSET, ASYNC_GENERATOR_QUEUE_OFFSET)
634514f5e3Sopenharmony_ci    ACCESSORS(AsyncGeneratorQueue, ASYNC_GENERATOR_QUEUE_OFFSET, GENERATOR_OFFSET)
644514f5e3Sopenharmony_ci    ACCESSORS(GeneratorBrand, GENERATOR_OFFSET, GENERATOR_RESUME_RESULT_OFFSET)
654514f5e3Sopenharmony_ci    ACCESSORS(ResumeResult, GENERATOR_RESUME_RESULT_OFFSET, BIT_FIELD_OFFSET)
664514f5e3Sopenharmony_ci    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
674514f5e3Sopenharmony_ci    DEFINE_ALIGN_SIZE(LAST_OFFSET);
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ci    // define Bitfield
704514f5e3Sopenharmony_ci    static constexpr size_t ASYNC_GENERATOE_STATE_BITS = 4;
714514f5e3Sopenharmony_ci    static constexpr size_t RESUME_MODE_BITS = 3;
724514f5e3Sopenharmony_ci    FIRST_BIT_FIELD(BitField, AsyncGeneratorState, JSAsyncGeneratorState, ASYNC_GENERATOE_STATE_BITS)
734514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, ResumeMode, AsyncGeneratorResumeMode, RESUME_MODE_BITS, AsyncGeneratorState)
744514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, GENERATOR_CONTEXT_OFFSET, BIT_FIELD_OFFSET)
754514f5e3Sopenharmony_ci    DECL_DUMP()
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci    // AsyncGeneratorValidate ( generator, generatorBrand )
784514f5e3Sopenharmony_ci    static void AsyncGeneratorValidate(JSThread *thread, const JSHandle<JSTaggedValue> &gen, const JSTaggedValue &val);
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    // AsyncGeneratorResolve ( generator, value, done )
814514f5e3Sopenharmony_ci    static JSTaggedValue AsyncGeneratorResolve(JSThread *thread, const JSHandle<JSAsyncGeneratorObject> &generator,
824514f5e3Sopenharmony_ci                                               const JSHandle<JSTaggedValue> value, bool done);
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci    static JSTaggedValue AsyncGeneratorReject(JSThread *thread, const JSHandle<JSAsyncGeneratorObject> &generator,
854514f5e3Sopenharmony_ci                                              const JSHandle<JSTaggedValue> value);
864514f5e3Sopenharmony_ci
874514f5e3Sopenharmony_ci    // AsyncGeneratorResumeNext ( generator )
884514f5e3Sopenharmony_ci    static JSTaggedValue AsyncGeneratorResumeNext(JSThread *thread, const JSHandle<JSAsyncGeneratorObject> &generator);
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci    // 27.6.3.7 AsyncGeneratorEnqueue ( generator, completion, generatorBrand )
914514f5e3Sopenharmony_ci    static JSTaggedValue AsyncGeneratorEnqueue(JSThread *thread, const JSHandle<JSTaggedValue> &generator,
924514f5e3Sopenharmony_ci                                               const JSHandle<CompletionRecord> completionRecord);
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    static JSTaggedValue PromiseResolve(JSThread *thread, const JSHandle<JSTaggedValue> promise,
954514f5e3Sopenharmony_ci                                        const JSHandle<JSTaggedValue> value);
964514f5e3Sopenharmony_ci
974514f5e3Sopenharmony_ci    static JSTaggedValue ProcessorFulfilledFunc(EcmaRuntimeCallInfo *argv);
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_ci    static JSTaggedValue ProcessorRejectedFunc(EcmaRuntimeCallInfo *argv);
1004514f5e3Sopenharmony_ci
1014514f5e3Sopenharmony_ci    inline bool IsSuspendYield() const
1024514f5e3Sopenharmony_ci    {
1034514f5e3Sopenharmony_ci        return GetAsyncGeneratorState() == JSAsyncGeneratorState::SUSPENDED_YIELD;
1044514f5e3Sopenharmony_ci    }
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ci    inline bool IsExecuting() const
1074514f5e3Sopenharmony_ci    {
1084514f5e3Sopenharmony_ci        return GetAsyncGeneratorState() == JSAsyncGeneratorState::EXECUTING;
1094514f5e3Sopenharmony_ci    }
1104514f5e3Sopenharmony_ci};
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ciclass JSAsyncGeneratorResNextRetProRstFtn : public JSFunction {
1134514f5e3Sopenharmony_cipublic:
1144514f5e3Sopenharmony_ci    CAST_CHECK(JSAsyncGeneratorResNextRetProRstFtn, IsJSAsyncGeneratorResNextRetProRstFtn);
1154514f5e3Sopenharmony_ci    static constexpr size_t ASYNC_GENERATOR_OBJECT_OFFSET = JSFunction::SIZE;
1164514f5e3Sopenharmony_ci    ACCESSORS(AsyncGeneratorObject, ASYNC_GENERATOR_OBJECT_OFFSET, SIZE);
1174514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSFunction, ASYNC_GENERATOR_OBJECT_OFFSET, SIZE)
1184514f5e3Sopenharmony_ci
1194514f5e3Sopenharmony_ci    DECL_DUMP()
1204514f5e3Sopenharmony_ci};
1214514f5e3Sopenharmony_ci}  // namespace ecmascript
1224514f5e3Sopenharmony_ci}  // namespace panda
1234514f5e3Sopenharmony_ci
1244514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_JS_ASYNC_GENERATOR_OBJECT_H
125