1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_JS_GENERATOR_OBJECT_H 17 #define ECMASCRIPT_JS_GENERATOR_OBJECT_H 18 19 #include "ecmascript/js_function.h" 20 21 namespace panda { 22 namespace ecmascript { 23 enum class JSGeneratorState : uint8_t { 24 UNDEFINED = 0, 25 SUSPENDED_START, 26 SUSPENDED_YIELD, 27 EXECUTING, 28 COMPLETED, 29 }; 30 enum class GeneratorResumeMode : uint8_t { 31 RETURN = 0, 32 THROW, 33 NEXT, 34 UNDEFINED 35 }; 36 37 class GeneratorContext : TaggedObject { 38 public: 39 CAST_CHECK(GeneratorContext, IsGeneratorContext); 40 41 static constexpr size_t GENERATOR_REGS_ARRAY_OFFSET = TaggedObjectSize(); 42 ACCESSORS(RegsArray, GENERATOR_REGS_ARRAY_OFFSET, GENERATOR_METHOD_OFFSET) 43 ACCESSORS(Method, GENERATOR_METHOD_OFFSET, GENERATOR_THIS_OFFSET) 44 ACCESSORS(This, GENERATOR_THIS_OFFSET, GENERATOR_ACC_OFFSET) 45 ACCESSORS(Acc, GENERATOR_ACC_OFFSET, GENERATOR_GENERATOR_OBJECT_OFFSET) 46 ACCESSORS(GeneratorObject, GENERATOR_GENERATOR_OBJECT_OFFSET, GENERATOR_LEXICALENV_OFFSET) 47 ACCESSORS(LexicalEnv, GENERATOR_LEXICALENV_OFFSET, GENERATOR_NREGS_OFFSET) 48 ACCESSORS_PRIMITIVE_FIELD(NRegs, uint32_t, GENERATOR_NREGS_OFFSET, GENERATOR_BC_OFFSET_OFFSET) 49 ACCESSORS_PRIMITIVE_FIELD(BCOffset, uint32_t, GENERATOR_BC_OFFSET_OFFSET, LAST_OFFSET) 50 DEFINE_ALIGN_SIZE(LAST_OFFSET); 51 52 DECL_VISIT_OBJECT(GENERATOR_REGS_ARRAY_OFFSET, GENERATOR_NREGS_OFFSET) 53 DECL_DUMP() 54 }; 55 56 class JSGeneratorObject : public JSObject { 57 public: 58 CAST_CHECK(JSGeneratorObject, IsGeneratorObject); 59 60 static constexpr size_t GENERATOR_CONTEXT_OFFSET = JSObject::SIZE; 61 ACCESSORS(GeneratorContext, GENERATOR_CONTEXT_OFFSET, GENERATOR_RESUME_RESULT_OFFSET) 62 ACCESSORS(ResumeResult, GENERATOR_RESUME_RESULT_OFFSET, BIT_FIELD_OFFSET) 63 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET) 64 DEFINE_ALIGN_SIZE(LAST_OFFSET); 65 66 // define BitField 67 static constexpr size_t GENERATOE_STATE_BITS = 3; 68 static constexpr size_t RESUME_MODE_BITS = 3; 69 FIRST_BIT_FIELD(BitField, GeneratorState, JSGeneratorState, GENERATOE_STATE_BITS) 70 NEXT_BIT_FIELD(BitField, ResumeMode, GeneratorResumeMode, RESUME_MODE_BITS, GeneratorState) 71 72 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, GENERATOR_CONTEXT_OFFSET, BIT_FIELD_OFFSET) 73 DECL_DUMP() 74 75 // 26.4.3.2 GeneratorValidate(generator) 76 static JSGeneratorState GeneratorValidate(JSThread *thread, const JSHandle<JSTaggedValue> &obj); 77 78 // 26.4.3.3 GeneratorResume(generator, value) 79 static JSHandle<JSObject> GeneratorResume(JSThread *thread, const JSHandle<JSGeneratorObject> &generator, 80 JSTaggedValue value); 81 82 // 26.4.3.4 GeneratorResumeAbrupt(generator, abruptCompletion) 83 static JSHandle<JSObject> GeneratorResumeAbrupt(JSThread *thread, const JSHandle<JSGeneratorObject> &generator, 84 const JSHandle<CompletionRecord> &abruptCompletion); 85 IsSuspendYield()86 inline bool IsSuspendYield() const 87 { 88 return GetGeneratorState() == JSGeneratorState::SUSPENDED_YIELD; 89 } 90 IsExecuting()91 inline bool IsExecuting() const 92 { 93 return GetGeneratorState() == JSGeneratorState::EXECUTING; 94 } 95 }; 96 97 class JSAsyncFuncObject : public JSGeneratorObject { 98 public: 99 CAST_CHECK(JSAsyncFuncObject, IsAsyncFuncObject); 100 101 static constexpr size_t GENERATOR_PROMISE_OFFSET = JSGeneratorObject::SIZE; 102 ACCESSORS(Promise, GENERATOR_PROMISE_OFFSET, SIZE); 103 104 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSGeneratorObject, GENERATOR_PROMISE_OFFSET, SIZE) 105 DECL_DUMP() 106 }; 107 } // namespace ecmascript 108 } // namespace panda 109 110 #endif // ECMASCRIPT_JS_GENERATOR_OBJECT_H 111