• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/ts_types/ts_manager.h"
17 #include <utility>
18 
19 #include "ecmascript/compiler/aot_file/aot_file_manager.h"
20 #include "ecmascript/global_env_constants-inl.h"
21 #include "ecmascript/jspandafile/class_literal.h"
22 #include "ecmascript/jspandafile/js_pandafile_manager.h"
23 #include "ecmascript/jspandafile/program_object.h"
24 #include "ecmascript/jspandafile/type_literal_extractor.h"
25 #include "ecmascript/ts_types/ts_type_table_generator.h"
26 #include "ecmascript/ts_types/ts_type_parser.h"
27 #include "ecmascript/vtable.h"
28 
29 namespace panda::ecmascript {
TSManager(EcmaVM * vm)30 TSManager::TSManager(EcmaVM *vm) : vm_(vm), thread_(vm_->GetJSThread()), factory_(vm_->GetFactory()),
31                                    assertTypes_(vm_->GetJSOptions().AssertTypes()),
32                                    typeThreshold_(vm_->GetJSOptions().GetTypeThreshold())
33 {
34     JSHandle<TSModuleTable> mTable = factory_->NewTSModuleTable(TSModuleTable::INITIAL_CAPACITY);
35     SetTSModuleTable(mTable);
36 }
37 
Initialize()38 void TSManager::Initialize()
39 {
40     TSTypeTableGenerator tableGenerator(this);
41     tableGenerator.GenerateDefaultTSTypeTables();
42 }
43 
GetHClassIndexByObjectType(const kungfu::GateType & gateType)44 int TSManager::GetHClassIndexByObjectType(const kungfu::GateType &gateType)
45 {
46     // make sure already setting correct curCP_ and curCPID_ before calling this method
47     if (!IsObjectTypeKind(gateType)) {
48         return -1;
49     }
50     GlobalTSTypeRef objectGT = gateType.GetGTRef();
51     return GetHClassIndex(objectGT);
52 }
53 
GetHClassIndexByInstanceGateType(const kungfu::GateType & gateType)54 int TSManager::GetHClassIndexByInstanceGateType(const kungfu::GateType &gateType)
55 {
56     // make sure already setting correct curCP_ and curCPID_ before calling this method
57     if (!IsClassInstanceTypeKind(gateType)) {
58         return -1;
59     }
60     GlobalTSTypeRef instanceGT = gateType.GetGTRef();
61     GlobalTSTypeRef classGT = GetClassType(instanceGT);
62     return GetHClassIndex(classGT);
63 }
64 
GetHClassIndexByClassGateType(const kungfu::GateType & gateType)65 int TSManager::GetHClassIndexByClassGateType(const kungfu::GateType &gateType)
66 {
67     // make sure already setting correct curCP_ and curCPID_ before calling this method
68     if (!IsClassTypeKind(gateType)) {
69         return -1;
70     }
71     GlobalTSTypeRef classGT = gateType.GetGTRef();
72     return GetHClassIndex(classGT);
73 }
74 
GetConstructorHClassIndexByClassGateType(const kungfu::GateType & gateType)75 int TSManager::GetConstructorHClassIndexByClassGateType(const kungfu::GateType &gateType)
76 {
77     // make sure already setting correct curCP_ and curCPID_ before calling this method
78     if (!IsClassTypeKind(gateType)) {
79         return -1;
80     }
81     GlobalTSTypeRef classGT = gateType.GetGTRef();
82     return GetHClassIndex(classGT, true);
83 }
84 
GetHClassIndex(GlobalTSTypeRef classGT,bool isConstructor)85 int TSManager::GetHClassIndex(GlobalTSTypeRef classGT, bool isConstructor)
86 {
87     if (HasOffsetFromGT(classGT)) {
88         uint32_t literalOffset = 0;
89         CString recordName = "";
90         CString abcNormalizedDesc = "";
91         std::tie(abcNormalizedDesc, recordName, literalOffset) = GetOffsetFromGt(classGT);
92         GetCompilationDriver()->AddResolvedMethod(recordName, literalOffset);
93     }
94     // make sure already setting correct curCP_ and curCPID_ before calling this method
95     std::map<GlobalTSTypeRef, IHClassData>::iterator iter;
96     std::map<GlobalTSTypeRef, IHClassData>::iterator endIter;
97     if (isConstructor) {
98         iter = gtConstructorhcMap_.find(classGT);
99         endIter = gtConstructorhcMap_.end();
100     } else {
101         iter = gtIhcMap_.find(classGT);
102         endIter = gtIhcMap_.end();
103     }
104     if (iter == endIter) {
105         return -1;
106     } else {
107         std::unordered_map<int32_t, uint32_t> &cpIndexMap = iter->second.GetCPIndexMap();
108         auto indexIter = cpIndexMap.find(curCPID_);
109         if (indexIter == cpIndexMap.end()) {
110             // This ihc is used in the current constantpool, but has not yet been recorded
111             return RecordIhcToVecAndIndexMap(iter->second);
112         }
113         return indexIter->second;
114     }
115 }
116 
RecordIhcToVecAndIndexMap(IHClassData & ihcData)117 uint32_t TSManager::RecordIhcToVecAndIndexMap([[maybe_unused]]IHClassData &ihcData)
118 {
119     // make sure already setting correct curCP_ and curCPID_ before calling this method
120     return -1;
121 }
122 
GetAOTHClassInfoByIndex(uint32_t index)123 JSTaggedValue TSManager::GetAOTHClassInfoByIndex([[maybe_unused]]uint32_t index)
124 {
125     // make sure already setting correct curCP_ and curCPID_ before calling this method
126     return JSTaggedValue::Undefined();
127 }
128 
GetExtendedClassType(JSHandle<TSClassType> classType) const129 JSHandle<TSClassType> TSManager::GetExtendedClassType(JSHandle<TSClassType> classType) const
130 {
131     ASSERT(classType.GetTaggedValue().IsTSClassType());
132     // Get extended type of classType based on ExtensionGT
133     GlobalTSTypeRef extensionGT = classType->GetExtensionGT();
134     JSHandle<JSTaggedValue> extendClassType = GetTSType(extensionGT);
135 
136     ASSERT(extendClassType->IsTSClassType());
137     return JSHandle<TSClassType>(extendClassType);
138 }
139 
GetExtendedClassType(const TSClassType * classType) const140 TSClassType *TSManager::GetExtendedClassType(const TSClassType *classType) const
141 {
142     DISALLOW_GARBAGE_COLLECTION;
143     ASSERT(JSTaggedValue(classType).IsTSClassType());
144     // Get extended type of classType based on ExtensionGT
145     GlobalTSTypeRef extensionGT = classType->GetExtensionGT();
146     JSHandle<JSTaggedValue> extendClassType = GetTSType(extensionGT);
147 
148     ASSERT(extendClassType->IsTSClassType());
149     return TSClassType::Cast(extendClassType->GetTaggedObject());
150 }
151 
GetPropType(GlobalTSTypeRef gt,JSHandle<JSTaggedValue> propertyName) const152 GlobalTSTypeRef TSManager::GetPropType(GlobalTSTypeRef gt, JSHandle<JSTaggedValue> propertyName) const
153 {
154     JSThread *thread = vm_->GetJSThread();
155     JSHandle<JSTaggedValue> type = GetTSType(gt);
156     ASSERT(type->IsTSType());
157 
158     if (type->IsTSClassType()) {
159         JSHandle<TSClassType> classType(type);
160         return TSClassType::GetPropTypeGT(thread, classType, propertyName);
161     } else if (type->IsTSClassInstanceType()) {
162         JSHandle<TSClassInstanceType> classInstanceType(type);
163         return TSClassInstanceType::GetPropTypeGT(thread, classInstanceType, propertyName);
164     } else if (type->IsTSObjectType()) {
165         JSHandle<TSObjectType> objectType(type);
166         return TSObjectType::GetPropTypeGT(thread, objectType, propertyName);
167     } else if (type->IsTSIteratorInstanceType()) {
168         JSHandle<TSIteratorInstanceType> iteratorInstance(type);
169         return TSIteratorInstanceType::GetPropTypeGT(thread, iteratorInstance, propertyName);
170     } else if (type->IsTSInterfaceType()) {
171         JSHandle<TSInterfaceType> interfaceType(type);
172         return TSInterfaceType::GetPropTypeGT(thread, interfaceType, propertyName);
173     } else if (type->IsTSNamespaceType()) {
174         JSHandle<TSNamespaceType> namespaceType(type);
175         return TSNamespaceType::GetPropTypeGT(thread, namespaceType, propertyName);
176     }
177     return GlobalTSTypeRef::Default();
178 }
179 
180 
GetIndexSignType(GlobalTSTypeRef objType,kungfu::GateType indexType) const181 GlobalTSTypeRef TSManager::GetIndexSignType(GlobalTSTypeRef objType, kungfu::GateType indexType) const
182 {
183     JSThread *thread = vm_->GetJSThread();
184     JSHandle<JSTaggedValue> type = GetTSType(objType);
185     ASSERT(type->IsTSType());
186 
187     uint32_t typeId = indexType.Value();
188     if (type->IsTSClassInstanceType()) {
189         JSHandle<TSClassInstanceType> classInstanceType(type);
190         return TSClassInstanceType::GetIndexSignType(thread, classInstanceType, typeId);
191     } else if (type->IsTSObjectType()) {
192         JSHandle<TSObjectType> objectType(type);
193         return TSObjectType::GetIndexSignType(thread, objectType, typeId);
194     } else if (type->IsTSInterfaceType()) {
195         JSHandle<TSInterfaceType> interfaceType(type);
196         return TSInterfaceType::GetIndexSignType(thread, interfaceType, typeId);
197     }
198     LOG_COMPILER(DEBUG) << "Unsupport TSType GetIndexSignType: "
199                         << static_cast<uint32_t>(type->GetTaggedObject()->GetClass()->GetObjectType());
200     return GlobalTSTypeRef::Default();
201 }
202 
IsStaticFunc(GlobalTSTypeRef gt) const203 bool TSManager::IsStaticFunc(GlobalTSTypeRef gt) const
204 {
205     ASSERT(IsFunctionTypeKind(gt));
206     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
207     ASSERT(tsType->IsTSFunctionType());
208     JSHandle<TSFunctionType> functionType(tsType);
209     return functionType->GetStatic();
210 }
211 
IsHotnessFunc(GlobalTSTypeRef gt) const212 bool TSManager::IsHotnessFunc(GlobalTSTypeRef gt) const
213 {
214     ASSERT(IsFunctionTypeKind(gt));
215     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
216     ASSERT(tsType->IsTSFunctionType());
217     JSHandle<TSFunctionType> functionType(tsType);
218     return functionType->GetIsHotness();
219 }
220 
SetHotnessFunc(GlobalTSTypeRef gt,bool isHotness) const221 void TSManager::SetHotnessFunc(GlobalTSTypeRef gt, bool isHotness) const
222 {
223     ASSERT(IsFunctionTypeKind(gt));
224     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
225     ASSERT(tsType->IsTSFunctionType());
226     JSHandle<TSFunctionType> functionType(tsType);
227     functionType->SetIsHotness(isHotness);
228 }
229 
GetSuperGateType(kungfu::GateType & gateType) const230 bool TSManager::GetSuperGateType(kungfu::GateType &gateType) const
231 {
232     JSHandle<JSTaggedValue> type = GetTSType(gateType.GetGTRef());
233     if (type->IsTSObjectType()) {
234         return false;
235     }
236     if (type->IsTSClassType()) {
237         JSHandle<TSClassType> classType(type);
238         if (classType->IsBaseClassType()) {
239             return false;
240         }
241         gateType = kungfu::GateType(classType->GetExtensionGT());
242         return true;
243     } else {
244         LOG_ECMA(FATAL) << "this branch is unreachable";
245         UNREACHABLE();
246     }
247 }
248 
GetSuperPropType(GlobalTSTypeRef gt,JSHandle<JSTaggedValue> propertyName,PropertyType propType) const249 GlobalTSTypeRef TSManager::GetSuperPropType(GlobalTSTypeRef gt, JSHandle<JSTaggedValue> propertyName,
250                                             PropertyType propType) const
251 {
252     JSThread *thread = vm_->GetJSThread();
253     JSHandle<JSTaggedValue> type = GetTSType(gt);
254     if (type->IsTSClassType()) {
255         JSHandle<TSClassType> classType(type);
256         return TSClassType::GetSuperPropTypeGT(thread, classType, propertyName, propType);
257     } else {
258         LOG_ECMA(FATAL) << "this branch is unreachable";
259         UNREACHABLE();
260     }
261 }
262 
GetSuperPropType(GlobalTSTypeRef gt,const uint64_t key,PropertyType propType) const263 GlobalTSTypeRef TSManager::GetSuperPropType(GlobalTSTypeRef gt, const uint64_t key, PropertyType propType) const
264 {
265     JSTaggedValue keyValue = JSTaggedValue(key);
266     JSMutableHandle<JSTaggedValue> propertyName(thread_, JSTaggedValue::Undefined());
267     if (keyValue.IsInt() || keyValue.IsDouble()) {
268         propertyName.Update(keyValue);
269     } else {
270         propertyName.Update(factory_->NewFromStdString(std::to_string(key).c_str()));
271     }
272     return GetSuperPropType(gt, propertyName, propType);
273 }
274 
GetPropType(GlobalTSTypeRef gt,const uint64_t key) const275 GlobalTSTypeRef TSManager::GetPropType(GlobalTSTypeRef gt, const uint64_t key) const
276 {
277     JSTaggedValue keyValue = JSTaggedValue(key);
278     JSMutableHandle<JSTaggedValue> propertyName(thread_, JSTaggedValue::Undefined());
279     if (keyValue.IsInt() || keyValue.IsDouble()) {
280         propertyName.Update(keyValue);
281     } else {
282         propertyName.Update(factory_->NewFromStdString(std::to_string(key).c_str()));
283     }
284     return GetPropType(gt, propertyName);
285 }
286 
GetUnionTypeLength(GlobalTSTypeRef gt) const287 uint32_t TSManager::GetUnionTypeLength(GlobalTSTypeRef gt) const
288 {
289     ASSERT(IsUnionTypeKind(gt));
290     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
291     ASSERT(tsType->IsTSUnionType());
292     JSHandle<TSUnionType> unionType = JSHandle<TSUnionType>(tsType);
293     JSHandle<TaggedArray> unionTypeArray(thread_, unionType->GetComponents());
294     return unionTypeArray->GetLength();
295 }
296 
GetUnionTypeByIndex(GlobalTSTypeRef gt,int index) const297 GlobalTSTypeRef TSManager::GetUnionTypeByIndex(GlobalTSTypeRef gt, int index) const
298 {
299     ASSERT(IsUnionTypeKind(gt));
300     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
301     ASSERT(tsType->IsTSUnionType());
302     JSHandle<TSUnionType> unionType = JSHandle<TSUnionType>(tsType);
303     JSHandle<TaggedArray> unionTypeArray(thread_, unionType->GetComponents());
304     uint32_t typeRawData = unionTypeArray->Get(index).GetInt();
305     return GlobalTSTypeRef(typeRawData);
306 }
307 
GetTypeKind(const GlobalTSTypeRef & gt) const308 TSTypeKind TSManager::GetTypeKind(const GlobalTSTypeRef &gt) const
309 {
310     if (!gt.IsPrimitiveModule()) {
311         JSHandle<JSTaggedValue> type = GetTSType(gt);
312         if (type->IsTSType()) {
313             JSHandle<TSType> tsType(type);
314             JSType hClassType = tsType->GetClass()->GetObjectType();
315             switch (hClassType) {
316                 case JSType::TS_CLASS_TYPE:
317                     return TSTypeKind::CLASS;
318                 case JSType::TS_CLASS_INSTANCE_TYPE:
319                     return TSTypeKind::CLASS_INSTANCE;
320                 case JSType::TS_FUNCTION_TYPE:
321                     return TSTypeKind::FUNCTION;
322                 case JSType::TS_UNION_TYPE:
323                     return TSTypeKind::UNION;
324                 case JSType::TS_ARRAY_TYPE:
325                     return TSTypeKind::ARRAY;
326                 case JSType::TS_OBJECT_TYPE:
327                     return TSTypeKind::OBJECT;
328                 case JSType::TS_INTERFACE_TYPE:
329                     return TSTypeKind::INTERFACE;
330                 case JSType::TS_ITERATOR_INSTANCE_TYPE:
331                     return TSTypeKind::ITERATOR_INSTANCE;
332                 case JSType::TS_NAMESPACE_TYPE:
333                     return TSTypeKind::NAMESPACE;
334                 default:
335                     LOG_ECMA(FATAL) << "this branch is unreachable";
336                     UNREACHABLE();
337             }
338         } else {
339             return TSTypeKind::UNKNOWN;
340         }
341     }
342     return TSTypeKind::PRIMITIVE;
343 }
344 
Dump()345 void TSManager::Dump()
346 {
347     std::cout << "TSTypeTables:";
348     JSHandle<TSModuleTable> table = GetTSModuleTable();
349     uint32_t GTLength = table->GetLength();
350     for (uint32_t i = 0; i < GTLength; i++) {
351         JSHandle<JSTaggedValue>(thread_, table->Get(i))->Dump(std::cout);
352     }
353 }
354 
GetOrCreateTSIteratorInstanceType(TSRuntimeType runtimeType,GlobalTSTypeRef elementGt)355 GlobalTSTypeRef TSManager::GetOrCreateTSIteratorInstanceType(TSRuntimeType runtimeType, GlobalTSTypeRef elementGt)
356 {
357     ASSERT((runtimeType >= TSRuntimeType::ITERATOR_RESULT) && (runtimeType <= TSRuntimeType::ITERATOR));
358     GlobalTSTypeRef kindGT =
359         GlobalTSTypeRef(static_cast<uint32_t>(ModuleTableIdx::RUNTIME), static_cast<int>(runtimeType));
360     GlobalTSTypeRef foundTypeRef = FindIteratorInstanceInInferTable(kindGT, elementGt);
361     if (!foundTypeRef.IsDefault()) {
362         return foundTypeRef;
363     }
364 
365     JSHandle<TSIteratorInstanceType> iteratorInstanceType = factory_->NewTSIteratorInstanceType();
366     iteratorInstanceType->SetKindGT(kindGT);
367     iteratorInstanceType->SetElementGT(elementGt);
368 
369     return AddTSTypeToInferredTable(JSHandle<TSType>(iteratorInstanceType));
370 }
371 
GetIteratorInstanceElementGt(GlobalTSTypeRef gt) const372 GlobalTSTypeRef TSManager::GetIteratorInstanceElementGt(GlobalTSTypeRef gt) const
373 {
374     ASSERT(IsIteratorInstanceTypeKind(gt));
375     JSHandle<JSTaggedValue> type = GetTSType(gt);
376     ASSERT(type->IsTSIteratorInstanceType());
377     JSHandle<TSIteratorInstanceType> iteratorFuncInstance(type);
378     GlobalTSTypeRef elementGT = iteratorFuncInstance->GetElementGT();
379     return elementGT;
380 }
381 
FindIteratorInstanceInInferTable(GlobalTSTypeRef kindGt,GlobalTSTypeRef elementGt) const382 GlobalTSTypeRef TSManager::FindIteratorInstanceInInferTable(GlobalTSTypeRef kindGt, GlobalTSTypeRef elementGt) const
383 {
384     DISALLOW_GARBAGE_COLLECTION;
385 
386     JSHandle<TSTypeTable> table = GetInferredTable();
387 
388     for (int index = 1; index <= table->GetNumberOfTypes(); ++index) {  // index 0 reseved for num of types
389         JSTaggedValue type = table->Get(index);
390         if (!type.IsTSIteratorInstanceType()) {
391             continue;
392         }
393 
394         TSIteratorInstanceType *insType = TSIteratorInstanceType::Cast(type.GetTaggedObject());
395         if (insType->GetKindGT() == kindGt && insType->GetElementGT() == elementGt) {
396             return insType->GetGT();
397         }
398     }
399 
400     return GlobalTSTypeRef::Default();  // not found
401 }
402 
AddTSTypeToTypeTable(const JSHandle<TSType> & type,int tableId) const403 GlobalTSTypeRef TSManager::AddTSTypeToTypeTable(const JSHandle<TSType> &type, int tableId) const
404 {
405     JSHandle<TSTypeTable> iTable = GetTSTypeTable(tableId);
406     if (UNLIKELY(!GlobalTSTypeRef::IsValidLocalId(iTable->GetNumberOfTypes() + 1))) {
407         LOG_COMPILER(DEBUG) << "The maximum number of TSTypes in TSTypeTable " << tableId << " is reached. ";
408         return GlobalTSTypeRef::Default();
409     }
410 
411     JSHandle<TSTypeTable> newITable = TSTypeTable::PushBackTypeToTable(thread_, iTable, type);
412     SetTSTypeTable(newITable, tableId);
413 
414     GlobalTSTypeRef gt = GlobalTSTypeRef(tableId, newITable->GetNumberOfTypes());
415     type->SetGT(gt);
416     return gt;
417 }
418 
FindUnionInTypeTable(JSHandle<TSTypeTable> table,JSHandle<TSUnionType> unionType) const419 GlobalTSTypeRef TSManager::FindUnionInTypeTable(JSHandle<TSTypeTable> table, JSHandle<TSUnionType> unionType) const
420 {
421     DISALLOW_GARBAGE_COLLECTION;
422     ASSERT(unionType.GetTaggedValue().IsTSUnionType());
423 
424     for (int index = 1; index <= table->GetNumberOfTypes(); ++index) {  // index 0 reseved for num of types
425         JSTaggedValue type = table->Get(index);
426         if (!type.IsTSUnionType()) {
427             continue;
428         }
429 
430         TSUnionType *uType = TSUnionType::Cast(type.GetTaggedObject());
431         if (uType->IsEqual(unionType)) {
432             return uType->GetGT();
433         }
434     }
435 
436     return GlobalTSTypeRef::Default();  // not found
437 }
438 
GetOrCreateUnionType(CVector<GlobalTSTypeRef> unionTypeVec)439 GlobalTSTypeRef TSManager::GetOrCreateUnionType(CVector<GlobalTSTypeRef> unionTypeVec)
440 {
441     uint32_t length = unionTypeVec.size();
442     JSHandle<TSUnionType> unionType = factory_->NewTSUnionType(length);
443     JSHandle<TaggedArray> components(thread_, unionType->GetComponents());
444     for (uint32_t unionArgIndex = 0; unionArgIndex < length; unionArgIndex++) {
445         components->Set(thread_, unionArgIndex, JSTaggedValue(unionTypeVec[unionArgIndex].GetType()));
446     }
447     unionType->SetComponents(thread_, components);
448 
449     JSHandle<TSModuleTable> mTable = GetTSModuleTable();
450     uint32_t numOfTables = static_cast<uint32_t>(mTable->GetNumberOfTSTypeTables());
451     for (uint32_t tableIndex = 0; tableIndex < numOfTables; ++tableIndex) {
452         JSHandle<TSTypeTable> typeTable = GetTSTypeTable(tableIndex);
453         GlobalTSTypeRef foundUnionRef = FindUnionInTypeTable(typeTable, unionType);
454         if (!foundUnionRef.IsDefault()) {
455             return foundUnionRef;
456         }
457     }
458 
459     return AddTSTypeToInferredTable(JSHandle<TSType>(unionType));
460 }
461 
Iterate(const RootVisitor & v)462 void TSManager::Iterate(const RootVisitor &v)
463 {
464     v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&globalModuleTable_)));
465     v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&curCP_)));
466     for (auto &iter : gtIhcMap_) {
467         iter.second.Iterate(v);
468     }
469     for (auto &exportTable : resolvedExportTable_) {
470         v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&resolvedExportTable_.at(exportTable.first))));
471     }
472 }
473 
GetTSTypeTable(int entry) const474 JSHandle<TSTypeTable> TSManager::GetTSTypeTable(int entry) const
475 {
476     JSHandle<TSModuleTable> mTable = GetTSModuleTable();
477     uint32_t tableOffset = TSModuleTable::GetTSTypeTableOffset(entry);
478     JSHandle<TSTypeTable> typeTable(thread_, mTable->Get(tableOffset));
479     return typeTable;
480 }
481 
SetTSTypeTable(const JSHandle<TSTypeTable> & table,int tableId) const482 void TSManager::SetTSTypeTable(const JSHandle<TSTypeTable> &table, int tableId) const
483 {
484     JSHandle<TSModuleTable> mTable = GetTSModuleTable();
485     uint32_t tableOffset = TSModuleTable::GetTSTypeTableOffset(tableId);
486     mTable->Set(thread_, tableOffset, table);
487 }
488 
GetFuncName(kungfu::GateType type) const489 std::string TSManager::GetFuncName(kungfu::GateType type) const
490 {
491     GlobalTSTypeRef gt = type.GetGTRef();
492     ASSERT(IsFunctionTypeKind(gt));
493     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
494     ASSERT(tsType->IsTSFunctionType());
495     JSHandle<TSFunctionType> functionType = JSHandle<TSFunctionType>(tsType);
496     auto name = functionType->GetName();
497     EcmaStringAccessor acc(name);
498     std::string nameStr = acc.ToStdString();
499     return nameStr;
500 }
501 
GetMethodIndex(GlobalTSTypeRef gt) const502 int TSManager::GetMethodIndex(GlobalTSTypeRef gt) const
503 {
504     ASSERT(IsFunctionTypeKind(gt));
505     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
506     JSHandle<TSFunctionType> functionType = JSHandle<TSFunctionType>(tsType);
507     uint32_t methodOffset = functionType->GetMethodOffset();
508     JSHandle<ConstantPool> constantPool(GetConstantPool());
509     return constantPool->GetMethodIndexByEntityId(panda_file::File::EntityId(methodOffset));
510 }
511 
GetFunctionTypeLength(GlobalTSTypeRef gt) const512 uint32_t TSManager::GetFunctionTypeLength(GlobalTSTypeRef gt) const
513 {
514     ASSERT(IsFunctionTypeKind(gt));
515     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
516     ASSERT(tsType->IsTSFunctionType());
517     JSHandle<TSFunctionType> functionType = JSHandle<TSFunctionType>(tsType);
518     return functionType->GetLength();
519 }
520 
GetFuncParameterTypeGT(GlobalTSTypeRef gt,int index) const521 GlobalTSTypeRef TSManager::GetFuncParameterTypeGT(GlobalTSTypeRef gt, int index) const
522 {
523     ASSERT(IsFunctionTypeKind(gt));
524     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
525     ASSERT(tsType->IsTSFunctionType());
526     JSHandle<TSFunctionType> functionType = JSHandle<TSFunctionType>(tsType);
527     return functionType->GetParameterTypeGT(index);
528 }
529 
GetFuncThisGT(GlobalTSTypeRef gt) const530 GlobalTSTypeRef TSManager::GetFuncThisGT(GlobalTSTypeRef gt) const
531 {
532     ASSERT(IsFunctionTypeKind(gt));
533     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
534     ASSERT(tsType->IsTSFunctionType());
535     JSHandle<TSFunctionType> functionType(tsType);
536     return functionType->GetThisGT();
537 }
538 
IsGetterSetterFunc(GlobalTSTypeRef gt) const539 bool TSManager::IsGetterSetterFunc(GlobalTSTypeRef gt) const
540 {
541     if (!IsFunctionTypeKind(gt)) {
542         return false;
543     }
544     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
545     ASSERT(tsType->IsTSFunctionType());
546     JSHandle<TSFunctionType> functionType(tsType);
547     return functionType->GetIsGetterSetter();
548 }
549 
IsAbstractMethod(GlobalTSTypeRef gt) const550 bool TSManager::IsAbstractMethod(GlobalTSTypeRef gt) const
551 {
552     if (!IsFunctionTypeKind(gt)) {
553         return false;
554     }
555     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
556     ASSERT(tsType->IsTSFunctionType());
557     JSHandle<TSFunctionType> functionType(tsType);
558     return functionType->GetIsAbstract();
559 }
560 
IsMethodSignature(GlobalTSTypeRef gt) const561 bool TSManager::IsMethodSignature(GlobalTSTypeRef gt) const
562 {
563     if (!IsFunctionTypeKind(gt)) {
564         return false;
565     }
566     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
567     ASSERT(tsType->IsTSFunctionType());
568     JSHandle<TSFunctionType> functionType(tsType);
569     return functionType->GetIsSignature();
570 }
571 
CanFastCall(GlobalTSTypeRef gt) const572 bool TSManager::CanFastCall(GlobalTSTypeRef gt) const
573 {
574     if (!IsFunctionTypeKind(gt)) {
575         return false;
576     }
577     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
578     ASSERT(tsType->IsTSFunctionType());
579     JSHandle<TSFunctionType> functionType(tsType);
580     return functionType->GetIsFastCall();
581 }
582 
IsNoGC(GlobalTSTypeRef gt) const583 bool TSManager::IsNoGC(GlobalTSTypeRef gt) const
584 {
585     if (!IsFunctionTypeKind(gt)) {
586         return false;
587     }
588     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
589     ASSERT(tsType->IsTSFunctionType());
590     JSHandle<TSFunctionType> functionType(tsType);
591     return functionType->GetIsNoGC();
592 }
593 
MethodOffsetIsVaild(GlobalTSTypeRef gt) const594 bool TSManager::MethodOffsetIsVaild(GlobalTSTypeRef gt) const
595 {
596     if (!IsFunctionTypeKind(gt)) {
597         return false;
598     }
599     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
600     ASSERT(tsType->IsTSFunctionType());
601     JSHandle<TSFunctionType> functionType(tsType);
602     return functionType->GetIsMethodOffsetVaild();
603 }
604 
FastCallFlagIsVaild(GlobalTSTypeRef gt) const605 bool TSManager::FastCallFlagIsVaild(GlobalTSTypeRef gt) const
606 {
607     if (!IsFunctionTypeKind(gt)) {
608         return false;
609     }
610     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
611     ASSERT(tsType->IsTSFunctionType());
612     JSHandle<TSFunctionType> functionType(tsType);
613     return functionType->GetIsFastCallVaild();
614 }
615 
GetFuncReturnValueTypeGT(GlobalTSTypeRef gt) const616 GlobalTSTypeRef TSManager::GetFuncReturnValueTypeGT(GlobalTSTypeRef gt) const
617 {
618     ASSERT(IsFunctionTypeKind(gt));
619     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
620     ASSERT(tsType->IsTSFunctionType());
621     JSHandle<TSFunctionType> functionType = JSHandle<TSFunctionType>(tsType);
622     return functionType->GetReturnGT();
623 }
624 
SetFuncMethodOffset(GlobalTSTypeRef gt,uint32_t methodIndex)625 void TSManager::SetFuncMethodOffset(GlobalTSTypeRef gt, uint32_t methodIndex)
626 {
627     ASSERT(GetTypeKind(gt) == TSTypeKind::FUNCTION);
628     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
629     ASSERT(tsType->IsTSFunctionType());
630     JSHandle<TSFunctionType> functionType = JSHandle<TSFunctionType>(tsType);
631     functionType->SetMethodOffset(methodIndex);
632 }
633 
GetFuncMethodOffset(GlobalTSTypeRef gt) const634 uint32_t TSManager::GetFuncMethodOffset(GlobalTSTypeRef gt) const
635 {
636     ASSERT(GetTypeKind(gt) == TSTypeKind::FUNCTION);
637     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
638     ASSERT(tsType->IsTSFunctionType());
639     JSHandle<TSFunctionType> functionType = JSHandle<TSFunctionType>(tsType);
640     return functionType->GetMethodOffset();
641 }
642 
CreateClassInstanceType(GlobalTSTypeRef gt)643 GlobalTSTypeRef TSManager::CreateClassInstanceType(GlobalTSTypeRef gt)
644 {
645     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
646     // handle buintin types if builtins.dts is not enabled
647     if (tsType->IsUndefined()) {
648         return GlobalTSTypeRef::Default();
649     }
650 
651     ASSERT(tsType->IsTSClassType());
652     JSHandle<TSClassInstanceType> classInstanceType = factory_->NewTSClassInstanceType();
653     classInstanceType->SetClassGT(gt);
654     return AddTSTypeToInferredTable(JSHandle<TSType>(classInstanceType));
655 }
656 
CreateArrayType()657 GlobalTSTypeRef TSManager::CreateArrayType()
658 {
659     JSHandle<TSArrayType> arrayType = factory_->NewTSArrayType();
660     return AddTSTypeToInferredTable(JSHandle<TSType>(arrayType));
661 }
662 
CreateNamespaceType()663 GlobalTSTypeRef TSManager::CreateNamespaceType()
664 {
665     JSHandle<TSNamespaceType> namespaceType = factory_->NewTSNamespaceType();
666     return AddTSTypeToInferredTable(JSHandle<TSType>(namespaceType));
667 }
668 
AddNamespacePropType(kungfu::GateType objType,JSTaggedValue name,kungfu::GateType valueType)669 bool TSManager::AddNamespacePropType(kungfu::GateType objType, JSTaggedValue name, kungfu::GateType valueType)
670 {
671     JSHandle<JSTaggedValue> tsType = GetTSType(GlobalTSTypeRef(objType.Value()));
672     JSHandle<TSNamespaceType> namespaceType(tsType);
673     JSHandle<JSTaggedValue> key(thread_, name);
674     JSHandle<JSTaggedValue> value(thread_, JSTaggedValue(valueType.Value()));
675     return TSNamespaceType::AddKeyAndValue(thread_, namespaceType, key, value);
676 }
677 
GetClassType(GlobalTSTypeRef classInstanceGT) const678 GlobalTSTypeRef TSManager::GetClassType(GlobalTSTypeRef classInstanceGT) const
679 {
680     ASSERT(IsClassInstanceTypeKind(classInstanceGT));
681     JSHandle<JSTaggedValue> tsType = GetTSType(classInstanceGT);
682     ASSERT(tsType->IsTSClassInstanceType());
683     JSHandle<TSClassInstanceType> instanceType(tsType);
684     return instanceType->GetClassGT();
685 }
686 
GetArrayParameterTypeGT(GlobalTSTypeRef gt) const687 GlobalTSTypeRef TSManager::GetArrayParameterTypeGT(GlobalTSTypeRef gt) const
688 {
689     ASSERT(IsArrayTypeKind(gt));
690     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
691     ASSERT(tsType->IsTSArrayType());
692     JSHandle<TSArrayType> arrayType = JSHandle<TSArrayType>(tsType);
693     return arrayType->GetElementGT();
694 }
695 
AddInstanceTSHClass(GlobalTSTypeRef gt,JSHandle<JSHClass> & ihclass)696 void TSManager::AddInstanceTSHClass(GlobalTSTypeRef gt, JSHandle<JSHClass> &ihclass)
697 {
698     IHClassData ihcData = IHClassData(ihclass.GetTaggedType());
699     gtIhcMap_.insert({gt, ihcData});
700 }
701 
AddConstructorTSHClass(GlobalTSTypeRef gt,JSHandle<JSHClass> & constructorHClass)702 void TSManager::AddConstructorTSHClass(GlobalTSTypeRef gt, JSHandle<JSHClass> &constructorHClass)
703 {
704     IHClassData ihcData = IHClassData(constructorHClass.GetTaggedType());
705     gtConstructorhcMap_.insert({gt, ihcData});
706 }
707 
GetTSHClass(const kungfu::GateType & gateType) const708 JSTaggedValue TSManager::GetTSHClass(const kungfu::GateType &gateType) const
709 {
710     if (!IsClassTypeKind(gateType) && !IsObjectTypeKind(gateType)) {
711         return JSTaggedValue::Hole();
712     }
713     GlobalTSTypeRef classGT = gateType.GetGTRef();
714     auto iter = gtIhcMap_.find(classGT);
715     if (iter != gtIhcMap_.end()) {
716         return JSTaggedValue(iter->second.GetIHC());
717     }
718     return JSTaggedValue::Hole();
719 }
720 
GetInstanceTSHClass(const JSHandle<TSClassType> & classType) const721 JSTaggedValue TSManager::GetInstanceTSHClass(const JSHandle<TSClassType> &classType) const
722 {
723     GlobalTSTypeRef gt = classType->GetGT();
724     IHClassData data = gtIhcMap_.at(gt);
725     return JSTaggedValue(data.GetIHC());
726 }
727 
HasTSHClass(const JSHandle<TSClassType> & classType) const728 bool TSManager::HasTSHClass(const JSHandle<TSClassType> &classType) const
729 {
730     GlobalTSTypeRef gt = classType->GetGT();
731     return gtIhcMap_.find(gt) != gtIhcMap_.end();
732 }
733 
HasTSHClass(const TSClassType * classType) const734 bool TSManager::HasTSHClass(const TSClassType *classType) const
735 {
736     GlobalTSTypeRef gt = classType->GetGT();
737     return gtIhcMap_.find(gt) != gtIhcMap_.end();
738 }
739 
GetTSType(const GlobalTSTypeRef & gt) const740 JSHandle<JSTaggedValue> TSManager::GetTSType(const GlobalTSTypeRef &gt) const
741 {
742     uint32_t moduleId = gt.GetModuleId();
743     uint32_t localId = gt.GetLocalId();
744 
745     if ((gt.IsBuiltinModule() && !IsBuiltinsDTSEnabled()) || gt.IsPrimitiveModule()) {
746         return thread_->GlobalConstants()->GetHandledUndefined();
747     }
748 
749     JSHandle<TSTypeTable> typeTable = GetTSTypeTable(moduleId);
750     JSHandle<JSTaggedValue> tsType(thread_, typeTable->Get(localId));
751     return tsType;
752 }
753 
IsBuiltinClassType(BuiltinTypeId id,kungfu::GateType gateType) const754 bool TSManager::IsBuiltinClassType(BuiltinTypeId id, kungfu::GateType gateType) const
755 {
756     if (!IsClassTypeKind(gateType)) {
757         return false;
758     }
759     auto classGT = gateType.GetGTRef();
760     if (UNLIKELY(!IsBuiltinsDTSEnabled())) {
761         uint32_t localId = classGT.GetLocalId();
762         return classGT.IsBuiltinModule() && (localId == static_cast<uint32_t>(id));
763     }
764 
765     const JSPandaFile *builtinPandaFile = GetBuiltinPandaFile();
766     uint32_t typeOffset = GetBuiltinOffset(static_cast<uint32_t>(id));
767     GlobalTypeID gId(builtinPandaFile, typeOffset);
768     if (HasCreatedGT(gId)) {
769         auto gt = GetGTByGlobalTypeID(gId);
770         return (gt == classGT);
771     }
772     return false;
773 }
774 
IsBuiltinInstanceType(BuiltinTypeId id,kungfu::GateType gateType) const775 bool TSManager::IsBuiltinInstanceType(BuiltinTypeId id, kungfu::GateType gateType) const
776 {
777     if (!IsClassInstanceTypeKind(gateType)) {
778         return false;
779     }
780     auto classGT = GetClassType(gateType.GetGTRef());
781     if (UNLIKELY(!IsBuiltinsDTSEnabled())) {
782         uint32_t localId = classGT.GetLocalId();
783         return classGT.IsBuiltinModule() && (localId == static_cast<uint32_t>(id));
784     }
785 
786     const JSPandaFile *builtinPandaFile = GetBuiltinPandaFile();
787     uint32_t typeOffset = GetBuiltinOffset(static_cast<uint32_t>(id));
788     GlobalTypeID gId(builtinPandaFile, typeOffset);
789     if (HasCreatedGT(gId)) {
790         auto gt = GetGTByGlobalTypeID(gId);
791         return (gt == classGT);
792     }
793     return false;
794 }
795 
IsTypedArrayType(kungfu::GateType gateType) const796 bool TSManager::IsTypedArrayType(kungfu::GateType gateType) const
797 {
798     if (!IsClassInstanceTypeKind(gateType)) {
799         return false;
800     }
801     const GlobalTSTypeRef gateGT = GlobalTSTypeRef(gateType.Value());
802     GlobalTSTypeRef classGT = GetClassType(gateGT);
803     if (IsBuiltinsDTSEnabled()) {
804         for (uint32_t i = static_cast<uint32_t>(BuiltinTypeId::TYPED_ARRAY_FIRST);
805              i <= static_cast<uint32_t>(BuiltinTypeId::TYPED_ARRAY_LAST); i++) {
806             GlobalTypeID gId(GetBuiltinPandaFile(), GetBuiltinOffset(i));
807             bool hasCreatedGT = HasCreatedGT(gId);
808             if (hasCreatedGT && (GetGTByGlobalTypeID(gId) == classGT)) {
809                 return true;
810             }
811         }
812         return false;
813     }
814     uint32_t l = classGT.GetLocalId();
815     return classGT.IsBuiltinModule() &&
816            (l >= static_cast<uint32_t>(BuiltinTypeId::TYPED_ARRAY_FIRST)) &&
817            (l <= static_cast<uint32_t>(BuiltinTypeId::TYPED_ARRAY_LAST));
818 }
819 
GetValidTypedArrayIds()820 const std::vector<BuiltinTypeId> &TSManager::GetValidTypedArrayIds()
821 {
822     static const std::vector<BuiltinTypeId> validTypedArrayIds = {
823         BuiltinTypeId::INT8_ARRAY,
824         BuiltinTypeId::UINT8_ARRAY,
825         BuiltinTypeId::UINT8_CLAMPED_ARRAY,
826         BuiltinTypeId::INT16_ARRAY,
827         BuiltinTypeId::UINT16_ARRAY,
828         BuiltinTypeId::INT32_ARRAY,
829         BuiltinTypeId::UINT32_ARRAY,
830         BuiltinTypeId::FLOAT32_ARRAY,
831         BuiltinTypeId::FLOAT64_ARRAY
832     };
833     return validTypedArrayIds;
834 }
835 
GetBuiltinTypeIdByJSType(JSType jsType)836 BuiltinTypeId TSManager::GetBuiltinTypeIdByJSType(JSType jsType)
837 {
838     static const std::map<JSType, BuiltinTypeId> jsTypeToBultinTypeID = {
839         {JSType::JS_INT8_ARRAY, BuiltinTypeId::INT8_ARRAY},
840         {JSType::JS_UINT8_ARRAY, BuiltinTypeId::UINT8_ARRAY},
841         {JSType::JS_UINT8_CLAMPED_ARRAY, BuiltinTypeId::UINT8_CLAMPED_ARRAY},
842         {JSType::JS_INT16_ARRAY, BuiltinTypeId::INT16_ARRAY},
843         {JSType::JS_UINT16_ARRAY, BuiltinTypeId::UINT16_ARRAY},
844         {JSType::JS_INT32_ARRAY, BuiltinTypeId::INT32_ARRAY},
845         {JSType::JS_UINT32_ARRAY, BuiltinTypeId::UINT32_ARRAY},
846         {JSType::JS_FLOAT32_ARRAY, BuiltinTypeId::FLOAT32_ARRAY},
847         {JSType::JS_FLOAT64_ARRAY, BuiltinTypeId::FLOAT64_ARRAY},
848     };
849 
850     auto it = jsTypeToBultinTypeID.find(jsType);
851     if (it != jsTypeToBultinTypeID.end()) {
852         return it->second;
853     }
854 
855     return BuiltinTypeId::NUM_INDEX_IN_SUMMARY;
856 }
857 
GetBuiltinsGateTypeByPt(ProfileType pgoType)858 const kungfu::GateType TSManager::GetBuiltinsGateTypeByPt(ProfileType pgoType)
859 {
860     ASSERT(pgoType.IsBuiltinsType());
861     JSType jsType = pgoType.GetBuiltinsId();
862 
863     auto it = pgoBuiltinGTCache_.find(jsType);
864     if (it != pgoBuiltinGTCache_.end()) {
865         return kungfu::GateType(it->second);
866     }
867 
868     BuiltinTypeId bTypeId = GetBuiltinTypeIdByJSType(jsType);
869     // If some builtin types sampled by pgo are not supported in AOT, AnyType will be returned
870     if (bTypeId == BuiltinTypeId::NUM_INDEX_IN_SUMMARY) {
871         return kungfu::GateType::AnyType();
872     }
873 
874     TSTypeParser parser(this);
875     GlobalTSTypeRef gt = parser.CreateGT(GetBuiltinPandaFile(), GetBuiltinRecordName(),
876         static_cast<uint32_t>(bTypeId));
877     GlobalTSTypeRef instanceGT;
878 
879     if (UNLIKELY(!IsBuiltinsDTSEnabled())) {
880         JSHandle<TSClassInstanceType> classInstanceType = factory_->NewTSClassInstanceType();
881         classInstanceType->SetClassGT(gt);
882         instanceGT = AddTSTypeToInferredTable(JSHandle<TSType>(classInstanceType));
883     } else {
884         instanceGT = CreateClassInstanceType(gt);
885     }
886 
887     pgoBuiltinGTCache_.insert({jsType, instanceGT});
888     return kungfu::GateType(instanceGT);
889 }
890 
GetTypedArrayBuiltinId(kungfu::GateType gateType) const891 BuiltinTypeId TSManager::GetTypedArrayBuiltinId(kungfu::GateType gateType) const
892 {
893     if (!IsClassInstanceTypeKind(gateType)) {
894         return BuiltinTypeId::NUM_INDEX_IN_SUMMARY;
895     }
896     const GlobalTSTypeRef gateGT = GlobalTSTypeRef(gateType.Value());
897     GlobalTSTypeRef classGT = GetClassType(gateGT);
898     const auto pandaFile = GetBuiltinPandaFile();
899     for (uint32_t i = static_cast<uint32_t>(BuiltinTypeId::TYPED_ARRAY_FIRST);
900             i <= static_cast<uint32_t>(BuiltinTypeId::TYPED_ARRAY_LAST); i++) {
901         if (IsBuiltinsDTSEnabled()) {
902             const auto offset = GetBuiltinOffset(i);
903             GlobalTypeID gId(pandaFile, offset);
904             if ((HasCreatedGT(gId)) &&
905                (GetGTByGlobalTypeID(gId) == classGT)) {
906                 return static_cast<BuiltinTypeId>(i);
907             }
908         }
909         uint32_t l = classGT.GetLocalId();
910         if (classGT.IsBuiltinModule() && (l == i)) {
911             return static_cast<BuiltinTypeId>(i);
912         }
913     }
914     return BuiltinTypeId::NUM_INDEX_IN_SUMMARY;
915 }
916 
IsValidTypedArrayType(kungfu::GateType gateType) const917 bool TSManager::IsValidTypedArrayType(kungfu::GateType gateType) const
918 {
919     std::vector<BuiltinTypeId> ids = GetValidTypedArrayIds();
920     for (const auto &id : ids) {
921         if (IsBuiltinInstanceType(id, gateType)) {
922             return true;
923         }
924     }
925     return false;
926 }
927 
IsIntTypedArrayType(kungfu::GateType gateType) const928 bool TSManager::IsIntTypedArrayType(kungfu::GateType gateType) const
929 {
930     return IsBuiltinInstanceType(BuiltinTypeId::INT8_ARRAY, gateType) ||
931            IsBuiltinInstanceType(BuiltinTypeId::UINT8_ARRAY, gateType) ||
932            IsBuiltinInstanceType(BuiltinTypeId::UINT8_CLAMPED_ARRAY, gateType) ||
933            IsBuiltinInstanceType(BuiltinTypeId::INT16_ARRAY, gateType) ||
934            IsBuiltinInstanceType(BuiltinTypeId::UINT16_ARRAY, gateType) ||
935            IsBuiltinInstanceType(BuiltinTypeId::INT32_ARRAY, gateType);
936 }
937 
IsDoubleTypedArrayType(kungfu::GateType gateType) const938 bool TSManager::IsDoubleTypedArrayType(kungfu::GateType gateType) const
939 {
940     return IsBuiltinInstanceType(BuiltinTypeId::FLOAT32_ARRAY, gateType) ||
941            IsBuiltinInstanceType(BuiltinTypeId::FLOAT64_ARRAY, gateType);
942 }
943 
GetBuiltinsName(uint32_t index) const944 std::string TSManager::GetBuiltinsName(uint32_t index) const
945 {
946     ASSERT(index >= static_cast<uint32_t>(BuiltinTypeId::FUNCTION) &&
947            index <=static_cast<uint32_t>(BuiltinTypeId::INTL));
948     BuiltinTypeId typeId = static_cast<BuiltinTypeId>(index);
949     switch (typeId) {
950         case BuiltinTypeId::FUNCTION:
951             return "Function";
952         case BuiltinTypeId::RANGE_ERROR:
953             return "RangeError";
954         case BuiltinTypeId::ERROR:
955             return "Error";
956         case BuiltinTypeId::OBJECT:
957             return "Object";
958         case BuiltinTypeId::SYNTAX_ERROR:
959             return "SyntaxError";
960         case BuiltinTypeId::TYPE_ERROR:
961             return "TypeError";
962         case BuiltinTypeId::REFERENCE_ERROR:
963             return "ReferenceError";
964         case BuiltinTypeId::URI_ERROR:
965             return "URIError";
966         case BuiltinTypeId::SYMBOL:
967             return "Symbol";
968         case BuiltinTypeId::EVAL_ERROR:
969             return "EvalError";
970         case BuiltinTypeId::NUMBER:
971             return "Number";
972         case BuiltinTypeId::PARSE_FLOAT:
973             return "parseFloat";
974         case BuiltinTypeId::DATE:
975             return "Date";
976         case BuiltinTypeId::BOOLEAN:
977             return "Boolean";
978         case BuiltinTypeId::BIG_INT:
979             return "BigInt";
980         case BuiltinTypeId::PARSE_INT:
981             return "parseInt";
982         case BuiltinTypeId::WEAK_MAP:
983             return "WeakMap";
984         case BuiltinTypeId::REG_EXP:
985             return "RegExp";
986         case BuiltinTypeId::SET:
987             return "Set";
988         case BuiltinTypeId::MAP:
989             return "Map";
990         case BuiltinTypeId::WEAK_REF:
991             return "WeakRef";
992         case BuiltinTypeId::WEAK_SET:
993             return "WeakSet";
994         case BuiltinTypeId::FINALIZATION_REGISTRY:
995             return "FinalizationRegistry";
996         case BuiltinTypeId::ARRAY:
997             return "Array";
998         case BuiltinTypeId::UINT8_CLAMPED_ARRAY:
999             return "Uint8ClampedArray";
1000         case BuiltinTypeId::UINT8_ARRAY:
1001             return "Uint8Array";
1002         case BuiltinTypeId::TYPED_ARRAY:
1003             return "TypedArray";
1004         case BuiltinTypeId::INT8_ARRAY:
1005             return "Int8Array";
1006         case BuiltinTypeId::UINT16_ARRAY:
1007             return "Uint16Array";
1008         case BuiltinTypeId::UINT32_ARRAY:
1009             return "Uint32Array";
1010         case BuiltinTypeId::INT16_ARRAY:
1011             return "Int16Array";
1012         case BuiltinTypeId::INT32_ARRAY:
1013             return "Int32Array";
1014         case BuiltinTypeId::FLOAT32_ARRAY:
1015             return "Float32Array";
1016         case BuiltinTypeId::FLOAT64_ARRAY:
1017             return "Float64Array";
1018         case BuiltinTypeId::BIGINT64_ARRAY:
1019             return "BigInt64Array";
1020         case BuiltinTypeId::BIGUINT64_ARRAY:
1021             return "BigUint64Array";
1022         case BuiltinTypeId::SHARED_ARRAY_BUFFER:
1023             return "SharedArrayBuffer";
1024         case BuiltinTypeId::DATA_VIEW:
1025             return "DataView";
1026         case BuiltinTypeId::STRING:
1027             return "String";
1028         case BuiltinTypeId::ARRAY_BUFFER:
1029             return "ArrayBuffer";
1030         case BuiltinTypeId::EVAL:
1031             return "eval";
1032         case BuiltinTypeId::IS_FINITE:
1033             return "isFinite";
1034         case BuiltinTypeId::ARK_PRIVATE:
1035             return "ArkPrivate";
1036         case BuiltinTypeId::PRINT:
1037             return "print";
1038         case BuiltinTypeId::DECODE_URI:
1039             return "decodeURI";
1040         case BuiltinTypeId::DECODE_URI_COMPONENT:
1041             return "decodeURIComponent";
1042         case BuiltinTypeId::IS_NAN:
1043             return "isNaN";
1044         case BuiltinTypeId::ENCODE_URI:
1045             return "encodeURI";
1046         case BuiltinTypeId::JS_NAN:
1047             return "NaN";
1048         case BuiltinTypeId::GLOBAL_THIS:
1049             return "globalThis";
1050         case BuiltinTypeId::ENCODE_URI_COMPONENT:
1051             return "encodeURIComponent";
1052         case BuiltinTypeId::JS_INFINITY:
1053             return "Infinity";
1054         case BuiltinTypeId::MATH:
1055             return "Math";
1056         case BuiltinTypeId::JSON:
1057             return "JSON";
1058         case BuiltinTypeId::ATOMICS:
1059             return "Atomics";
1060         case BuiltinTypeId::UNDEFINED:
1061             return "undefined";
1062         case BuiltinTypeId::REFLECT:
1063             return "Reflect";
1064         case BuiltinTypeId::PROMISE:
1065             return "Promise";
1066         case BuiltinTypeId::PROXY:
1067             return "Proxy";
1068         case BuiltinTypeId::GENERATOR_FUNCTION:
1069             return "GeneratorFunction";
1070         case BuiltinTypeId::INTL:
1071             return "Intl";
1072         default:
1073             UNREACHABLE();
1074     }
1075 }
1076 
GetBuiltinIndex(GlobalTSTypeRef builtinGT) const1077 uint32_t TSManager::GetBuiltinIndex(GlobalTSTypeRef builtinGT) const
1078 {
1079     ASSERT(builtinGT.IsBuiltinModule());
1080     if (IsBuiltinsDTSEnabled()) {
1081         for (uint32_t idx = static_cast<uint32_t>(BuiltinTypeId::FUNCTION);
1082             idx <= static_cast<uint32_t>(BuiltinTypeId::INTL); idx++) {
1083             GlobalTypeID gId(GetBuiltinPandaFile(), GetBuiltinOffset(idx));
1084             if ((HasCreatedGT(gId)) &&
1085                (GetGTByGlobalTypeID(gId) == builtinGT)) {
1086                    return idx;
1087                }
1088         }
1089     }
1090     return builtinGT.GetLocalId();
1091 }
1092 
GetClassTypeStr(GlobalTSTypeRef gt) const1093 std::string TSManager::GetClassTypeStr(GlobalTSTypeRef gt) const
1094 {
1095     if (gt.IsBuiltinModule()) {
1096         uint32_t index = GetBuiltinIndex(gt);
1097         return GetBuiltinsName(index);
1098     }
1099 
1100     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
1101     if (tsType->IsUndefined()) {
1102         return "any";
1103     }
1104     ASSERT(tsType->IsTSClassType());
1105     JSHandle<TSClassType> classType = JSHandle<TSClassType>(tsType);
1106     JSTaggedValue taggedValue = classType->GetName();
1107     EcmaStringAccessor accessor(taggedValue);
1108     return accessor.ToStdString();
1109 }
1110 
GetClassInstanceTypeStr(GlobalTSTypeRef gt) const1111 std::string TSManager::GetClassInstanceTypeStr(GlobalTSTypeRef gt) const
1112 {
1113     JSHandle<JSTaggedValue> tsType = GetTSType(gt);
1114     ASSERT(tsType->IsTSClassInstanceType());
1115     JSHandle<TSClassInstanceType> classInstance(tsType);
1116     return GetClassTypeStr(classInstance->GetClassGT());
1117 }
1118 
GetFunctionTypeStr(GlobalTSTypeRef gt) const1119 std::string TSManager::GetFunctionTypeStr(GlobalTSTypeRef gt) const
1120 {
1121     std::string functionStr = "(";
1122     uint32_t parameterLength = GetFunctionTypeLength(gt);
1123     for (uint32_t i = 0; i < parameterLength; i++) {
1124         GlobalTSTypeRef parameterGT = GetFuncParameterTypeGT(gt, i);
1125         std::string parameterStr = GetTypeStr(kungfu::GateType(parameterGT));
1126         if (i != parameterLength - 1) {
1127             functionStr = functionStr + parameterStr + ", ";
1128         } else {
1129             functionStr = functionStr + parameterStr;
1130         }
1131     }
1132     GlobalTSTypeRef returnGT = GetFuncReturnValueTypeGT(gt);
1133     std::string returnStr = GetTypeStr(kungfu::GateType(returnGT));
1134     functionStr = functionStr + ") => " + returnStr;
1135     return functionStr;
1136 }
1137 
GetArrayTypeStr(GlobalTSTypeRef gt) const1138 std::string TSManager::GetArrayTypeStr(GlobalTSTypeRef gt) const
1139 {
1140     GlobalTSTypeRef elementGt = GetArrayParameterTypeGT(gt);
1141     std::string arrayStr = GetTypeStr(kungfu::GateType(elementGt)) + "[]";
1142     return arrayStr;
1143 }
1144 
GetTypeStr(kungfu::GateType gateType) const1145 std::string TSManager::GetTypeStr(kungfu::GateType gateType) const
1146 {
1147     GlobalTSTypeRef gt = gateType.GetGTRef();
1148     auto typeKind = GetTypeKind(gt);
1149     switch (typeKind) {
1150         case TSTypeKind::PRIMITIVE:
1151             return GetPrimitiveStr(gt);
1152         case TSTypeKind::CLASS:
1153             return "typeof " + GetClassTypeStr(gt);
1154         case TSTypeKind::CLASS_INSTANCE:
1155             return GetClassInstanceTypeStr(gt);
1156         case TSTypeKind::FUNCTION:
1157             return GetFunctionTypeStr(gt);
1158         case TSTypeKind::UNION:
1159             return "union";
1160         case TSTypeKind::ARRAY:
1161             return GetArrayTypeStr(gt);
1162         case TSTypeKind::OBJECT:
1163             return "object";
1164         case TSTypeKind::IMPORT:
1165             return "import";
1166         case TSTypeKind::INTERFACE:
1167             return "interface";
1168         case TSTypeKind::ITERATOR_INSTANCE:
1169             return "iterator_instance";
1170         case TSTypeKind::UNKNOWN:
1171             return "unknown";
1172         case TSTypeKind::NAMESPACE:
1173             return "namespace";
1174         default:
1175             LOG_ECMA(FATAL) << "this branch is unreachable";
1176             UNREACHABLE();
1177     }
1178 }
1179 
GetPrimitiveStr(const GlobalTSTypeRef & gt) const1180 std::string TSManager::GetPrimitiveStr(const GlobalTSTypeRef &gt) const
1181 {
1182     auto primitive = static_cast<TSPrimitiveType>(gt.GetLocalId());
1183     switch (primitive) {
1184         case TSPrimitiveType::ANY:
1185             return "any";
1186         case TSPrimitiveType::NUMBER:
1187             return "number";
1188         case TSPrimitiveType::BOOLEAN:
1189             return "boolean";
1190         case TSPrimitiveType::VOID_TYPE:
1191             return "void";
1192         case TSPrimitiveType::STRING:
1193             return "string";
1194         case TSPrimitiveType::SYMBOL:
1195             return "symbol";
1196         case TSPrimitiveType::NULL_TYPE:
1197             return "null";
1198         case TSPrimitiveType::UNDEFINED:
1199             return "undefined";
1200         case TSPrimitiveType::INT:
1201             return "int";
1202         case TSPrimitiveType::DOUBLE:
1203             return "double";
1204         case TSPrimitiveType::BIG_INT:
1205             return "bigint";
1206         default:
1207             return "unknown";
1208     }
1209 }
1210 
SetCurConstantPool(const JSPandaFile * jsPandaFile,uint32_t methodOffset)1211 void TSManager::SetCurConstantPool(const JSPandaFile *jsPandaFile, uint32_t methodOffset)
1212 {
1213     curCPID_ = GetConstantPoolIDByMethodOffset(jsPandaFile, methodOffset);
1214     curCP_ = vm_->GetJSThread()->GetCurrentEcmaContext()->FindConstpool(jsPandaFile, curCPID_);
1215     curJSPandaFile_ = jsPandaFile;
1216 }
1217 
GetConstantPoolIDByMethodOffset(const JSPandaFile * jsPandaFile,uint32_t methodOffset)1218 int32_t TSManager::GetConstantPoolIDByMethodOffset(const JSPandaFile *jsPandaFile, uint32_t methodOffset)
1219 {
1220     panda_file::IndexAccessor indexAccessor(*jsPandaFile->GetPandaFile(),
1221                                             panda_file::File::EntityId(methodOffset));
1222     return static_cast<int32_t>(indexAccessor.GetHeaderIndex());
1223 }
1224 
TryNarrowUnionType(kungfu::GateType gateType)1225 kungfu::GateType TSManager::TryNarrowUnionType(kungfu::GateType gateType)
1226 {
1227     // for unionType like A|null B|undefined, even A|null|undefined, we will narrow it to A or B at some situations.
1228     // such as ldobjbyname, stobjbyvalue etc.
1229     if (IsUnionTypeKind(gateType)) {
1230         JSHandle<JSTaggedValue> type = GetTSType(gateType.GetGTRef());
1231         JSHandle<TSUnionType> unionType(type);
1232         JSHandle<TaggedArray> components(thread_, unionType->GetComponents());
1233         auto length = components->GetLength();
1234         if (length > 3) { // the Maximum shrinkable union size is 3 in extreme scenario like A | null | undefined.
1235             return gateType;
1236         }
1237         auto actualTypeNum = 0;
1238         kungfu::GateType actualType = kungfu::GateType::AnyType();
1239         for (uint32_t index = 0; index < length; index++) {
1240             kungfu::GateType typeGt = kungfu::GateType(components->Get(index).GetInt());
1241             if (!typeGt.IsNullType() && !typeGt.IsUndefinedType()) {
1242                 actualTypeNum++;
1243                 actualType = typeGt;
1244                 if (actualTypeNum > 1) { // if there isn't a single actual type, we can't narrow the union type.
1245                     break;
1246                 }
1247             }
1248         }
1249         if (actualTypeNum == 1) {
1250             return actualType;
1251         }
1252     }
1253     return gateType;
1254 }
1255 
GetConstantPoolId(uint32_t methodOffset) const1256 uint32_t TSManager::GetConstantPoolId(uint32_t methodOffset) const
1257 {
1258     panda_file::IndexAccessor indexAccessor(*(curJSPandaFile_->GetPandaFile()),
1259                                             panda_file::File::EntityId(methodOffset));
1260     return static_cast<uint32_t>(indexAccessor.GetHeaderIndex());
1261 }
1262 
GetConstantPool(uint32_t methodOffset) const1263 JSTaggedValue TSManager::GetConstantPool(uint32_t methodOffset) const
1264 {
1265     uint32_t cpId = GetConstantPoolId(methodOffset);
1266     return thread_->GetCurrentEcmaContext()->FindConstpool(curJSPandaFile_, cpId);
1267 }
1268 
GetStringFromConstantPool(uint32_t methodId,const uint16_t stringId) const1269 JSTaggedValue TSManager::GetStringFromConstantPool(uint32_t methodId, const uint16_t stringId) const
1270 {
1271     JSHandle<ConstantPool> constantPool(thread_, GetConstantPool(methodId));
1272     return ConstantPool::GetStringFromCache(thread_, constantPool.GetTaggedValue(), stringId);
1273 }
1274 
GetExportTableFromLiteral(const JSPandaFile * jsPandaFile,const CString & recordName)1275 JSHandle<TaggedArray> TSManager::GetExportTableFromLiteral(const JSPandaFile *jsPandaFile, const CString &recordName)
1276 {
1277     // To compare with the exportTable, we need to parse the literalbuffer in abc TypeAnnotation.
1278     // If the exportTable already exist, we will use it directly. OtherWise, we will parse and store it.
1279     // In type system parser at a later stage, we will also use these arrays to avoid duplicate parsing.
1280     if (HasResolvedExportTable(jsPandaFile, recordName)) {
1281         JSTaggedValue exportTypeTable = GetResolvedExportTable(jsPandaFile, recordName);
1282         JSHandle<TaggedArray> table(vm_->GetJSThread(), exportTypeTable);
1283         return table;
1284     }
1285 
1286     JSHandle<TaggedArray> typeOfExportedSymbols = GenerateExportTableFromLiteral(jsPandaFile, recordName);
1287     AddResolvedExportTable(jsPandaFile, recordName, typeOfExportedSymbols.GetTaggedValue());
1288     return typeOfExportedSymbols;
1289 }
1290 
GenerateExportTableFromLiteral(const JSPandaFile * jsPandaFile,const CString & recordName)1291 JSHandle<TaggedArray> TSManager::GenerateExportTableFromLiteral(const JSPandaFile *jsPandaFile,
1292                                                                 const CString &recordName)
1293 {
1294     bool isBuiltinTable = (std::strcmp(recordName.c_str(), TSTypeTable::BUILTINS_TABLE_NAME) == 0);
1295     ExportTypeTableExtractor exTableExtractor(jsPandaFile, recordName, isBuiltinTable);
1296     uint32_t length = exTableExtractor.GetLength();
1297     JSHandle<TaggedArray> typeOfExportedSymbols = factory_->NewOldSpaceTaggedArray(length);
1298     uint32_t index = 0;
1299     exTableExtractor.EnumerateModuleTypes(
1300         [this, &typeOfExportedSymbols, &index](const CString &name, const uint32_t typeId) {
1301             JSHandle<EcmaString> str = factory_->NewFromUtf8(name);
1302             typeOfExportedSymbols->Set(thread_, index++, JSTaggedValue(str.GetTaggedValue()));
1303             typeOfExportedSymbols->Set(thread_, index++, JSTaggedValue(typeId));
1304         });
1305     return typeOfExportedSymbols;
1306 }
1307 
IsBuiltinObjectMethod(BuiltinTypeId id,kungfu::GateType funcType) const1308 bool TSManager::IsBuiltinObjectMethod(BuiltinTypeId id, kungfu::GateType funcType) const
1309 {
1310     DISALLOW_GARBAGE_COLLECTION;
1311     GlobalTSTypeRef funcGT = funcType.GetGTRef();
1312     if (!funcGT.IsBuiltinModule()) {
1313         return false;
1314     }
1315 
1316     if (IsBuiltinsDTSEnabled()) {
1317         uint32_t idx = static_cast<uint32_t>(id);
1318         const JSPandaFile *builtinPandaFile = GetBuiltinPandaFile();
1319         uint32_t builtinOffset = GetBuiltinOffset(idx);
1320         GlobalTypeID gId(builtinPandaFile, builtinOffset);
1321         bool hasCreatedGT = HasCreatedGT(gId);
1322         if (hasCreatedGT) {
1323             JSHandle<JSTaggedValue> funcTsType = GetTSType(funcGT);
1324             ASSERT(funcTsType->IsTSFunctionType());
1325             JSHandle<TSFunctionType> functionType = JSHandle<TSFunctionType>(funcTsType);
1326             auto name = functionType->GetName();
1327 
1328             auto gt = GetGTByGlobalTypeID(gId);
1329             auto tsType = GetTSType(gt);
1330             ASSERT(tsType->IsTSClassType());
1331             JSHandle<TSClassType> classType(tsType);
1332             JSHandle<TSObjectType> constructorType(thread_, classType->GetConstructorType());
1333             JSTaggedValue layout = constructorType->GetObjLayoutInfo();
1334             TSObjLayoutInfo *itLayout = TSObjLayoutInfo::Cast(layout.GetTaggedObject());
1335             int index = itLayout->GetElementIndexByKey(name);
1336             if (index != -1) {
1337                 auto builtinFuncGt = GlobalTSTypeRef(itLayout->GetTypeId(index).GetInt());
1338                 return builtinFuncGt == funcGT;
1339             }
1340             JSHandle<TSObjectType> prototypeType(thread_, classType->GetPrototypeType());
1341             JSTaggedValue prototypeLayout = prototypeType->GetObjLayoutInfo();
1342             TSObjLayoutInfo *pPrototypeLayout = TSObjLayoutInfo::Cast(prototypeLayout.GetTaggedObject());
1343             index = pPrototypeLayout->GetElementIndexByKey(name);
1344             if (index != -1) {
1345                 auto builtinFuncGt = GlobalTSTypeRef(pPrototypeLayout->GetTypeId(index).GetInt());
1346                 return builtinFuncGt == funcGT;
1347             }
1348         }
1349     }
1350     return false;
1351 }
1352 
GenerateBuiltinSummary()1353 void TSManager::GenerateBuiltinSummary()
1354 {
1355     ASSERT(IsBuiltinsDTSEnabled());
1356     CString builtinsDTSFileName = GetBuiltinsDTS();
1357     std::shared_ptr<JSPandaFile> jsPandaFile = JSPandaFileManager::GetInstance()->OpenJSPandaFile(
1358         builtinsDTSFileName, panda::ecmascript::TSTypeTable::DEFAULT_TYPE_VIRTUAL_NAME);
1359     if (jsPandaFile == nullptr) {
1360         LOG_COMPILER(FATAL) << "load lib_ark_builtins.d.ts failed";
1361     }
1362     JSPandaFileManager::GetInstance()->AddJSPandaFileVm(vm_, jsPandaFile);
1363     SetBuiltinPandaFile(jsPandaFile.get());
1364     CString builtinsRecordName(TSTypeTable::BUILTINS_TABLE_NAME);
1365     SetBuiltinRecordName(builtinsRecordName);
1366     TypeSummaryExtractor summExtractor(jsPandaFile.get(), builtinsRecordName);
1367     summExtractor.EnumerateTypeOffsets(static_cast<uint32_t>(BuiltinTypeId::NUM_OF_BUILTIN_TYPES),
1368         [this](const uint32_t typeOffset) {
1369             builtinOffsets_.emplace_back(typeOffset);
1370         });
1371 }
1372 
PrintNumOfTypes() const1373 void TSManager::PrintNumOfTypes() const
1374 {
1375     JSHandle<TSModuleTable> mTable = GetTSModuleTable();
1376     uint32_t length = static_cast<uint32_t>(mTable->GetNumberOfTSTypeTables());
1377     uint32_t totalNumOfTypes = 0;
1378     for (uint32_t i = 0; i < length; i++) {
1379         JSHandle<EcmaString> valueString = mTable->GetModuleRequestByModuleId(thread_, i);
1380         std::string name = EcmaStringAccessor(valueString).ToStdString();
1381         valueString = mTable->GetAbcRequestByModuleId(thread_, i);
1382         std::string abcName = EcmaStringAccessor(valueString).ToStdString();
1383         JSHandle<TSTypeTable> tTable = GetTSTypeTable(i);
1384         uint32_t numOfExpectedTypes = static_cast<uint32_t>(tTable->GetNumberOfTypes());
1385         uint32_t numOfTypes = 0;
1386         for (uint32_t j = 1; j <= numOfExpectedTypes; j++) {
1387             JSHandle<JSTaggedValue> tsType(thread_, tTable->Get(j));
1388             if (tsType->IsTSType()) {
1389                 numOfTypes++;
1390             }
1391         }
1392         totalNumOfTypes += numOfTypes;
1393         LOG_COMPILER(DEBUG) << "module table: " << i << ", "
1394                             << "abc name: " << abcName << ", "
1395                             << "module name: " << name << ", "
1396                             << "number of types: " << numOfTypes;
1397     }
1398     LOG_COMPILER(DEBUG) << "total number of types: " << totalNumOfTypes;
1399 }
1400 
PrintTypeInfo(const JSPandaFile * jsPandaFile) const1401 void TSManager::PrintTypeInfo(const JSPandaFile *jsPandaFile) const
1402 {
1403     if (!(vm_->GetJSOptions().PrintTypeInfo())) {
1404         return;
1405     }
1406 
1407     LOG_COMPILER(INFO) << "====================================================================";
1408     LOG_COMPILER(INFO) << "start printing type info in file " << jsPandaFile->GetFileName();
1409     const auto &records = jsPandaFile->GetJSRecordInfo();
1410     for (const auto &it : records) {
1411         const auto &recordName = it.first;
1412         LOG_COMPILER(INFO) << "====================================================================";
1413         LOG_COMPILER(INFO) << "In record " << recordName;
1414         if (jsPandaFile->HasTypeSummaryOffset(recordName)) {
1415             LOG_COMPILER(INFO) << "[TypeLiterals]";
1416             TypeSummaryExtractor(jsPandaFile, recordName).Print();
1417         }
1418         ExportTypeTableExtractor(jsPandaFile, recordName, false).Print();
1419     }
1420     LOG_COMPILER(INFO) << "====================================================================";
1421     LOG_COMPILER(INFO) << "end of printing type info";
1422 }
1423 
GetModuleRequestByModuleId(JSThread * thread,int entry) const1424 JSHandle<EcmaString> TSModuleTable::GetModuleRequestByModuleId(JSThread *thread, int entry) const
1425 {
1426     int amiOffset = GetModuleRequestOffset(entry);
1427     JSHandle<EcmaString> amiPath(thread, Get(amiOffset));
1428     return amiPath;
1429 }
1430 
GetAbcRequestByModuleId(JSThread * thread,int entry) const1431 JSHandle<EcmaString> TSModuleTable::GetAbcRequestByModuleId(JSThread *thread, int entry) const
1432 {
1433     int amiOffset = GetAbcRequestOffset(entry);
1434     JSHandle<EcmaString> abcPath(thread, Get(amiOffset));
1435     return abcPath;
1436 }
1437 
GetGlobalModuleID(JSThread * thread,JSHandle<EcmaString> amiPath,JSHandle<EcmaString> abcPath) const1438 int TSModuleTable::GetGlobalModuleID(JSThread *thread, JSHandle<EcmaString> amiPath, JSHandle<EcmaString> abcPath) const
1439 {
1440     uint32_t length = GetNumberOfTSTypeTables();
1441     for (uint32_t i = 0; i < length; i++) {
1442         JSHandle<EcmaString> moduleString = GetModuleRequestByModuleId(thread, i);
1443         JSHandle<EcmaString> abcName = GetAbcRequestByModuleId(thread, i);
1444         if (EcmaStringAccessor::StringsAreEqual(*amiPath, *moduleString) &&
1445             EcmaStringAccessor::StringsAreEqual(*abcPath, *abcName)) {
1446             return i;
1447         }
1448     }
1449     return NOT_FOUND;
1450 }
1451 
IsBuiltinConstructor(BuiltinTypeId id,GlobalTSTypeRef ctorGT) const1452 bool TSManager::IsBuiltinConstructor(BuiltinTypeId id, GlobalTSTypeRef ctorGT) const
1453 {
1454     ASSERT(ctorGT.IsBuiltinModule());
1455     if (IsBuiltinsDTSEnabled()) {
1456         uint32_t idx = static_cast<uint32_t>(id);
1457         const JSPandaFile *builtinPandaFile = GetBuiltinPandaFile();
1458         uint32_t builtinOffset = GetBuiltinOffset(idx);
1459         GlobalTypeID gId(builtinPandaFile, builtinOffset);
1460         bool hasCreatedGT = HasCreatedGT(gId);
1461         if (hasCreatedGT) {
1462             auto gt = GetGTByGlobalTypeID(gId);
1463             return ctorGT == gt;
1464         }
1465     }
1466 
1467     return false;
1468 }
1469 } // namespace panda::ecmascript
1470