1 /* 2 * Copyright (c) 2023 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_TS_TYPES_TS_TYPE_ACCESSOR_H 17 #define ECMASCRIPT_TS_TYPES_TS_TYPE_ACCESSOR_H 18 19 #include "ecmascript/ts_types/ts_manager.h" 20 #include "ecmascript/ts_types/ts_type.h" 21 22 namespace panda::ecmascript { 23 #define GET_TSTYPE_LIST(V) \ 24 V(Class, JSHandle<TSClassType>) \ 25 V(ClassInstance, JSHandle<TSClassInstanceType>) \ 26 V(Function, JSHandle<TSFunctionType>) \ 27 V(Union, JSHandle<TSUnionType>) \ 28 V(Array, JSHandle<TSArrayType>) \ 29 V(Object, JSHandle<TSObjectType>) \ 30 V(Interface, JSHandle<TSInterfaceType>) \ 31 V(IteratorInstance, JSHandle<TSIteratorInstanceType>) \ 32 V(Namespace, JSHandle<TSNamespaceType>) 33 34 #define CLASS_TYPE_BITFIELD_ACCESSOR_LIST(V) \ 35 V(HasLinked) \ 36 V(HasPassedSubtypingCheck) \ 37 V(HasAnalysedInitialization) \ 38 V(HasEscapedThisInConstructor) \ 39 V(HasSuperCallInConstructor) 40 41 class TSFieldAttributes { 42 public: TSFieldAttributes(uint32_t value)43 explicit TSFieldAttributes(uint32_t value) : value_(value) {} 44 ~TSFieldAttributes() = default; 45 NO_COPY_SEMANTIC(TSFieldAttributes); 46 NO_MOVE_SEMANTIC(TSFieldAttributes); 47 48 static constexpr size_t BOOL_FLAG_BIT_LENGTH = 1; 49 FIRST_BIT_FIELD(Value, InitializedFlag, bool, BOOL_FLAG_BIT_LENGTH); 50 GetValue()51 inline uint32_t GetValue() const 52 { 53 return value_; 54 } 55 SetValue(const uint32_t value)56 inline void SetValue(const uint32_t value) 57 { 58 value_ = value; 59 } 60 61 private: 62 uint32_t value_ {0}; 63 }; 64 65 class TSTypeAccessor { 66 public: TSTypeAccessor(TSManager * tsManager,const GlobalTSTypeRef gt)67 explicit TSTypeAccessor(TSManager *tsManager, const GlobalTSTypeRef gt) 68 : tsManager_(tsManager), thread_(tsManager_->GetThread()), gt_(gt) {} 69 TSTypeAccessor(TSManager * tsManager,const kungfu::GateType type)70 explicit TSTypeAccessor(TSManager *tsManager, const kungfu::GateType type) 71 : tsManager_(tsManager), thread_(tsManager_->GetThread()), gt_(type.Value()) {} 72 73 ~TSTypeAccessor() = default; 74 NO_COPY_SEMANTIC(TSTypeAccessor); 75 NO_MOVE_SEMANTIC(TSTypeAccessor); 76 77 void PUBLIC_API MarkPropertyInitialized(JSTaggedValue key); 78 79 void PUBLIC_API UpdateNonStaticProp(JSTaggedValue key, GlobalTSTypeRef newGT); 80 81 void PUBLIC_API UpdateStaticProp(JSTaggedValue key, GlobalTSTypeRef newGT); 82 83 void PUBLIC_API UpdateForEachCBPara(kungfu::GateType targetType); 84 85 bool PUBLIC_API IsPropertyInitialized(JSTaggedValue key) const; 86 87 std::string PUBLIC_API GetInitializedProperties() const; 88 89 std::string PUBLIC_API GetClassTypeName() const; 90 91 std::string PUBLIC_API GetFunctionName() const; 92 93 GlobalTSTypeRef PUBLIC_API GetAccessorGT(JSTaggedValue key, bool isSetter) const; 94 95 #define CLASS_TYPE_BITFIELD_ACCESSOR(NAME) \ 96 inline void PUBLIC_API MarkClass##NAME() \ 97 { \ 98 ASSERT(tsManager_->IsClassTypeKind(gt_)); \ 99 JSHandle<TSClassType> classType = GetClassType(); \ 100 classType->Set##NAME(true); \ 101 } \ 102 \ 103 inline bool PUBLIC_API Class##NAME() const \ 104 { \ 105 ASSERT(tsManager_->IsClassTypeKind(gt_)); \ 106 JSHandle<TSClassType> classType = GetClassType(); \ 107 return classType->Get##NAME(); \ 108 } 109 110 CLASS_TYPE_BITFIELD_ACCESSOR_LIST(CLASS_TYPE_BITFIELD_ACCESSOR) 111 #undef CLASS_TYPE_BITFIELD_ACCESSOR 112 113 private: 114 #define GET_TSTYPE(NAME, TYPE) \ 115 inline TYPE Get##NAME##Type() const \ 116 { \ 117 ASSERT(tsManager_->Is##NAME##TypeKind(gt_)); \ 118 JSHandle<JSTaggedValue> tsType = tsManager_->GetTSType(gt_); \ 119 return TYPE(tsType); \ 120 } 121 122 GET_TSTYPE_LIST(GET_TSTYPE) 123 #undef GET_TSTYPE 124 125 JSTaggedValue MarkInitialized(JSTaggedValue flag); 126 127 JSHandle<TSObjLayoutInfo> GetInstanceTypeLayout() const; 128 JSHandle<TSObjLayoutInfo> GetPrototypeTypeLayout() const; 129 IsGetterGT(size_t parameterLength,bool isSetter)130 bool IsGetterGT(size_t parameterLength, bool isSetter) const 131 { 132 return parameterLength == 0 && !isSetter; 133 } 134 IsSetterGT(size_t parameterLength,bool isSetter)135 bool IsSetterGT(size_t parameterLength, bool isSetter) const 136 { 137 return parameterLength == 1 && isSetter; 138 } 139 140 TSManager *tsManager_ {nullptr}; 141 JSThread *thread_ {nullptr}; 142 GlobalTSTypeRef gt_; 143 }; 144 #undef CLASS_TYPE_BITFIELD_ACCESSOR_LIST 145 #undef GET_TSTYPE_LIST 146 } // panda::ecmascript 147 #endif // ECMASCRIPT_TS_TYPES_TS_TYPE_ACCESSOR_H 148