• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/ts_types/ts_type_accessor.h"
17 
18 namespace panda::ecmascript {
MarkPropertyInitialized(JSTaggedValue key)19 void TSTypeAccessor::MarkPropertyInitialized(JSTaggedValue key)
20 {
21     ASSERT(tsManager_->IsClassTypeKind(gt_));
22     JSHandle<TSObjLayoutInfo> layout = GetInstanceTypeLayout();
23     int elemenIndex = layout->GetElementIndexByKey(key);
24     if (TSObjLayoutInfo::IsValidIndex(elemenIndex)) {
25         JSTaggedValue attribute = layout->GetAttribute(elemenIndex);
26         ASSERT(attribute.IsInt());
27         layout->SetAttribute(thread_, elemenIndex, MarkInitialized(attribute));
28     }
29 }
30 
UpdateNonStaticProp(JSTaggedValue key,GlobalTSTypeRef newGT)31 void TSTypeAccessor::UpdateNonStaticProp(JSTaggedValue key, GlobalTSTypeRef newGT)
32 {
33     ASSERT(tsManager_->IsClassTypeKind(gt_));
34     JSHandle<TSClassType> classType = GetClassType();
35     JSHandle<JSTaggedValue> propName(thread_, key);
36     TSClassType::UpdateNonStaticPropTypeGT(thread_, classType, propName, newGT);
37 }
38 
UpdateStaticProp(JSTaggedValue key,GlobalTSTypeRef newGT)39 void TSTypeAccessor::UpdateStaticProp(JSTaggedValue key, GlobalTSTypeRef newGT)
40 {
41     ASSERT(tsManager_->IsClassTypeKind(gt_));
42     JSHandle<TSClassType> classType = GetClassType();
43     JSHandle<JSTaggedValue> propName(thread_, key);
44     TSClassType::UpdateStaticPropTypeGT(thread_, classType, propName, newGT);
45 }
46 
UpdateForEachCBPara(kungfu::GateType targetType)47 void TSTypeAccessor::UpdateForEachCBPara(kungfu::GateType targetType)
48 {
49     if (!tsManager_->IsFunctionTypeKind(gt_)) {
50         return;
51     }
52     JSHandle<TSFunctionType> callbackType = GetFunctionType();
53     uint32_t paraSz = callbackType->GetLength();
54     JSHandle<TaggedArray> parameterTypes(thread_, callbackType->GetParameterTypes());
55     const uint32_t maxParaSz = 3; // elementValue, index and array
56     if (paraSz > maxParaSz) {
57         return;
58     }
59     GlobalTSTypeRef indeGT = kungfu::GateType::IntType().GetGTRef();
60     GlobalTSTypeRef elementGT = GlobalTSTypeRef::Default();
61     GlobalTSTypeRef targetGT = targetType.GetGTRef();
62     if (tsManager_->IsArrayTypeKind(targetType)) {
63         elementGT = tsManager_->GetArrayParameterTypeGT(targetType);
64     }
65     if (tsManager_->IsTypedArrayType(targetType)) {
66         elementGT = kungfu::GateType::NumberType().GetGTRef();
67     }
68     if (elementGT.IsDefault()) {
69         return;
70     }
71     std::vector<GlobalTSTypeRef> tempGTs{elementGT, indeGT, targetGT};
72     for (uint32_t i = 0; i < paraSz; i++) {
73         if (!parameterTypes->Get(i).GetInt()) {
74             parameterTypes->Set(thread_, i, JSTaggedValue(tempGTs[i].GetType()));
75         }
76     }
77 }
78 
MarkInitialized(JSTaggedValue attribute)79 JSTaggedValue TSTypeAccessor::MarkInitialized(JSTaggedValue attribute)
80 {
81     ASSERT(attribute.IsInt());
82     TSFieldAttributes fieldAttr(static_cast<uint32_t>(attribute.GetInt()));
83     fieldAttr.SetInitializedFlag(true);
84     return JSTaggedValue(fieldAttr.GetValue());
85 }
86 
IsPropertyInitialized(JSTaggedValue key) const87 bool TSTypeAccessor::IsPropertyInitialized(JSTaggedValue key) const
88 {
89     ASSERT(tsManager_->IsClassTypeKind(gt_));
90     JSHandle<TSObjLayoutInfo> layout = GetInstanceTypeLayout();
91     int elemenIndex = layout->GetElementIndexByKey(key);
92     if (!TSObjLayoutInfo::IsValidIndex(elemenIndex)) {
93         return false;
94     }
95     JSTaggedValue attribute = layout->GetAttribute(elemenIndex);
96     ASSERT(attribute.IsInt());
97     TSFieldAttributes fieldAttr(static_cast<uint32_t>(attribute.GetInt()));
98     return fieldAttr.GetInitializedFlag();
99 }
100 
GetInitializedProperties() const101 std::string TSTypeAccessor::GetInitializedProperties() const
102 {
103     ASSERT(tsManager_->IsClassTypeKind(gt_));
104     JSHandle<TSObjLayoutInfo> layout = GetInstanceTypeLayout();
105     std::string names("");
106     uint32_t length = layout->GetNumOfProperties();
107     for (uint32_t i = 0; i < length; i++) {
108         JSTaggedValue attribute = layout->GetAttribute(i);
109         ASSERT(attribute.IsInt());
110         TSFieldAttributes fieldAttr(static_cast<uint32_t>(attribute.GetInt()));
111         if (fieldAttr.GetInitializedFlag()) {
112             JSTaggedValue name = layout->GetKey(i);
113             ASSERT(name.IsString());
114             names += (EcmaStringAccessor(name).ToStdString() + " ");
115         }
116     }
117     return names;
118 }
119 
GetClassTypeName() const120 std::string TSTypeAccessor::GetClassTypeName() const
121 {
122     ASSERT(tsManager_->IsClassTypeKind(gt_));
123     return tsManager_->GetClassTypeStr(gt_);
124 }
125 
GetFunctionName() const126 std::string TSTypeAccessor::GetFunctionName() const
127 {
128     JSHandle<TSFunctionType> funcType = GetFunctionType();
129     EcmaStringAccessor acc(funcType->GetName());
130     std::string nameStr = acc.ToStdString();
131     return nameStr;
132 }
133 
GetInstanceTypeLayout() const134 JSHandle<TSObjLayoutInfo> TSTypeAccessor::GetInstanceTypeLayout() const
135 {
136     ASSERT(tsManager_->IsClassTypeKind(gt_));
137     JSHandle<TSClassType> classType = GetClassType();
138     JSHandle<TSObjectType> instanceType(thread_, classType->GetInstanceType());
139     JSHandle<TSObjLayoutInfo> layout(thread_, instanceType->GetObjLayoutInfo());
140     return layout;
141 }
142 
GetPrototypeTypeLayout() const143 JSHandle<TSObjLayoutInfo> TSTypeAccessor::GetPrototypeTypeLayout() const
144 {
145     ASSERT(tsManager_->IsClassTypeKind(gt_));
146     JSHandle<TSClassType> classType = GetClassType();
147     JSHandle<TSObjectType> prototypeType(thread_, classType->GetPrototypeType());
148     JSHandle<TSObjLayoutInfo> layout(thread_, prototypeType->GetObjLayoutInfo());
149     return layout;
150 }
151 
GetPrototypePropGT(JSTaggedValue key) const152 GlobalTSTypeRef TSTypeAccessor::GetPrototypePropGT(JSTaggedValue key) const
153 {
154     ASSERT(tsManager_->IsClassTypeKind(gt_));
155     JSHandle<TSObjLayoutInfo> layout = GetPrototypeTypeLayout();
156     int index = layout->GetElementIndexByKey(key);
157     if (TSObjLayoutInfo::IsValidIndex(index)) {
158         return GlobalTSTypeRef(layout->GetTypeId(index).GetInt());
159     }
160     return GlobalTSTypeRef();
161 }
162 }  // namespace panda::ecmascript
163