14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2024 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_GENERATOR_OBJECT_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_JS_GENERATOR_OBJECT_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/accessor_data.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_object.h"
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_cinamespace panda {
234514f5e3Sopenharmony_cinamespace ecmascript {
244514f5e3Sopenharmony_cienum class JSGeneratorState : uint8_t {
254514f5e3Sopenharmony_ci    UNDEFINED = 0,
264514f5e3Sopenharmony_ci    SUSPENDED_START,
274514f5e3Sopenharmony_ci    SUSPENDED_YIELD,
284514f5e3Sopenharmony_ci    EXECUTING,
294514f5e3Sopenharmony_ci    COMPLETED,
304514f5e3Sopenharmony_ci};
314514f5e3Sopenharmony_cienum class GeneratorResumeMode : uint8_t {
324514f5e3Sopenharmony_ci    RETURN = 0,
334514f5e3Sopenharmony_ci    THROW,
344514f5e3Sopenharmony_ci    NEXT,
354514f5e3Sopenharmony_ci    UNDEFINED
364514f5e3Sopenharmony_ci};
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_ciclass GeneratorContext : TaggedObject {
394514f5e3Sopenharmony_cipublic:
404514f5e3Sopenharmony_ci    CAST_CHECK(GeneratorContext, IsGeneratorContext);
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci    static constexpr size_t GENERATOR_REGS_ARRAY_OFFSET = TaggedObjectSize();
434514f5e3Sopenharmony_ci    ACCESSORS(RegsArray, GENERATOR_REGS_ARRAY_OFFSET, GENERATOR_METHOD_OFFSET)
444514f5e3Sopenharmony_ci    ACCESSORS(Method, GENERATOR_METHOD_OFFSET, GENERATOR_THIS_OFFSET)
454514f5e3Sopenharmony_ci    ACCESSORS(This, GENERATOR_THIS_OFFSET, GENERATOR_ACC_OFFSET)
464514f5e3Sopenharmony_ci    ACCESSORS(Acc, GENERATOR_ACC_OFFSET, GENERATOR_GENERATOR_OBJECT_OFFSET)
474514f5e3Sopenharmony_ci    ACCESSORS(GeneratorObject, GENERATOR_GENERATOR_OBJECT_OFFSET, GENERATOR_LEXICALENV_OFFSET)
484514f5e3Sopenharmony_ci    ACCESSORS(LexicalEnv, GENERATOR_LEXICALENV_OFFSET, GENERATOR_NREGS_OFFSET)
494514f5e3Sopenharmony_ci    ACCESSORS_PRIMITIVE_FIELD(NRegs, uint32_t, GENERATOR_NREGS_OFFSET, GENERATOR_BC_OFFSET_OFFSET)
504514f5e3Sopenharmony_ci    ACCESSORS_PRIMITIVE_FIELD(BCOffset, uint32_t, GENERATOR_BC_OFFSET_OFFSET, LAST_OFFSET)
514514f5e3Sopenharmony_ci    DEFINE_ALIGN_SIZE(LAST_OFFSET);
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT(GENERATOR_REGS_ARRAY_OFFSET, GENERATOR_NREGS_OFFSET)
544514f5e3Sopenharmony_ci    DECL_DUMP()
554514f5e3Sopenharmony_ci};
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ciclass JSGeneratorObject : public JSObject {
584514f5e3Sopenharmony_cipublic:
594514f5e3Sopenharmony_ci    CAST_CHECK(JSGeneratorObject, IsGeneratorObject);
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci    static constexpr size_t GENERATOR_CONTEXT_OFFSET = JSObject::SIZE;
624514f5e3Sopenharmony_ci    ACCESSORS(GeneratorContext, GENERATOR_CONTEXT_OFFSET, GENERATOR_RESUME_RESULT_OFFSET)
634514f5e3Sopenharmony_ci    ACCESSORS(ResumeResult, GENERATOR_RESUME_RESULT_OFFSET, TASK_INFO_OFFSET)
644514f5e3Sopenharmony_ci    ACCESSORS_PRIMITIVE_FIELD(TaskInfo, uintptr_t, TASK_INFO_OFFSET, BIT_FIELD_OFFSET)
654514f5e3Sopenharmony_ci    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
664514f5e3Sopenharmony_ci    DEFINE_ALIGN_SIZE(LAST_OFFSET);
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    // define BitField
694514f5e3Sopenharmony_ci    static constexpr size_t GENERATOE_STATE_BITS = 3;
704514f5e3Sopenharmony_ci    static constexpr size_t RESUME_MODE_BITS = 3;
714514f5e3Sopenharmony_ci    FIRST_BIT_FIELD(BitField, GeneratorState, JSGeneratorState, GENERATOE_STATE_BITS)
724514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, ResumeMode, GeneratorResumeMode, RESUME_MODE_BITS, GeneratorState)
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, GENERATOR_CONTEXT_OFFSET, TASK_INFO_OFFSET)
754514f5e3Sopenharmony_ci    DECL_DUMP()
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci    // 26.4.3.2 GeneratorValidate(generator)
784514f5e3Sopenharmony_ci    static JSGeneratorState GeneratorValidate(JSThread *thread, const JSHandle<JSTaggedValue> &obj);
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    // 26.4.3.3 GeneratorResume(generator, value)
814514f5e3Sopenharmony_ci    static JSHandle<JSObject> GeneratorResume(JSThread *thread, const JSHandle<JSGeneratorObject> &generator,
824514f5e3Sopenharmony_ci                                              JSTaggedValue value);
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci    // 26.4.3.4 GeneratorResumeAbrupt(generator, abruptCompletion)
854514f5e3Sopenharmony_ci    static JSHandle<JSObject> GeneratorResumeAbrupt(JSThread *thread, const JSHandle<JSGeneratorObject> &generator,
864514f5e3Sopenharmony_ci                                                    const JSHandle<CompletionRecord> &abruptCompletion);
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    inline bool IsSuspendYield() const
894514f5e3Sopenharmony_ci    {
904514f5e3Sopenharmony_ci        return GetGeneratorState() == JSGeneratorState::SUSPENDED_YIELD;
914514f5e3Sopenharmony_ci    }
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_ci    inline bool IsExecuting() const
944514f5e3Sopenharmony_ci    {
954514f5e3Sopenharmony_ci        return GetGeneratorState() == JSGeneratorState::EXECUTING;
964514f5e3Sopenharmony_ci    }
974514f5e3Sopenharmony_ci};
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_ciclass JSAsyncFuncObject : public JSGeneratorObject {
1004514f5e3Sopenharmony_cipublic:
1014514f5e3Sopenharmony_ci    CAST_CHECK(JSAsyncFuncObject, IsAsyncFuncObject);
1024514f5e3Sopenharmony_ci
1034514f5e3Sopenharmony_ci    static constexpr size_t GENERATOR_PROMISE_OFFSET = JSGeneratorObject::SIZE;
1044514f5e3Sopenharmony_ci    ACCESSORS(Promise, GENERATOR_PROMISE_OFFSET, SIZE);
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSGeneratorObject, GENERATOR_PROMISE_OFFSET, SIZE)
1074514f5e3Sopenharmony_ci    DECL_DUMP()
1084514f5e3Sopenharmony_ci};
1094514f5e3Sopenharmony_ci}  // namespace ecmascript
1104514f5e3Sopenharmony_ci}  // namespace panda
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_JS_GENERATOR_OBJECT_H
113