diff --git a/ecmascript/dfx/hprof/heap_snapshot.cpp b/ecmascript/dfx/hprof/heap_snapshot.cpp index 17ab6e7d308a439b876d3f744c0642b6a9707b48..c6aa840d90e0e53261a8d3e836383c8e6e39cd5a 100644 --- a/ecmascript/dfx/hprof/heap_snapshot.cpp +++ b/ecmascript/dfx/hprof/heap_snapshot.cpp @@ -557,6 +557,8 @@ CString *HeapSnapshot::GenerateNodeName(TaggedObject *entry) return GetString("TSFunctionType"); case JSType::TS_ARRAY_TYPE: return GetString("TSArrayType"); + case JSType::TS_MODULE_NAMESPACE_TYPE: + return GetString("TSModuleNameSpaceType"); default: break; } diff --git a/ecmascript/dump.cpp b/ecmascript/dump.cpp index a66f969399088483f02074d0dcd9e1b134feff41..02809ad78e40a8be51887f81a608171d1fa0dd0e 100644 --- a/ecmascript/dump.cpp +++ b/ecmascript/dump.cpp @@ -368,6 +368,8 @@ CString JSHClass::DumpJSType(JSType type) return "TSFunctionType"; case JSType::TS_ARRAY_TYPE: return "TSArrayType"; + case JSType::TS_MODULE_NAMESPACE_TYPE: + return "TSModuleNameSpaceType"; case JSType::TS_ITERATOR_INSTANCE_TYPE: return "TSIteratorInstanceType"; case JSType::JS_API_ARRAYLIST_ITERATOR: @@ -955,6 +957,9 @@ static void DumpObject(TaggedObject *obj, std::ostream &os) case JSType::TS_ARRAY_TYPE: TSArrayType::Cast(obj)->Dump(os); break; + case JSType::TS_MODULE_NAMESPACE_TYPE: + TSModuleNameSpaceType::Cast(obj)->Dump(os); + return; case JSType::TS_ITERATOR_INSTANCE_TYPE: TSIteratorInstanceType::Cast(obj)->Dump(os); break; @@ -3348,6 +3353,25 @@ void TSIteratorInstanceType::Dump(std::ostream &os) const os << "\n"; } +void TSModuleNameSpaceType::Dump(std::ostream &os) const +{ + os << " - TSModuleNameSpaceType globalTSTypeRef: "; + GlobalTSTypeRef gt = GetGT(); + uint64_t globalTSTypeRef = gt.GetType(); + os << globalTSTypeRef; + os << "\n"; + os << " - TSModuleNameSpaceType moduleId: "; + uint32_t moduleId = gt.GetModuleId(); + os << moduleId; + os << "\n"; + os << " - TSModuleNameSpaceType localTypeId: "; + uint32_t localTypeId = gt.GetLocalId(); + os << localTypeId; + os << "\n"; + os << " - ExportArray: "; + DumpArrayClass(TaggedArray::Cast(GetExportArray().GetTaggedObject()), os); +} + void SourceTextModule::Dump(std::ostream &os) const { os << " - Environment: "; @@ -4050,6 +4074,9 @@ static void DumpObject(TaggedObject *obj, case JSType::TS_ARRAY_TYPE: TSArrayType::Cast(obj)->DumpForSnapshot(vec); return; + case JSType::TS_MODULE_NAMESPACE_TYPE: + TSModuleNameSpaceType::Cast(obj)->DumpForSnapshot(vec); + return; case JSType::TS_ITERATOR_INSTANCE_TYPE: TSIteratorInstanceType::Cast(obj)->DumpForSnapshot(vec); return; @@ -5250,6 +5277,11 @@ void TSIteratorInstanceType::DumpForSnapshot(std::vector> &vec) const +{ + vec.emplace_back("ExportArray", GetExportArray()); +} + void SourceTextModule::DumpForSnapshot(std::vector> &vec) const { // please update the NUM_OF_ITEMS if you change the items below diff --git a/ecmascript/global_env_constants.cpp b/ecmascript/global_env_constants.cpp index 190152f23961675d7e048b2ef4e147db40f992f1..d09891dadbb6b228e8a33ae4a1543e2e7195fada 100644 --- a/ecmascript/global_env_constants.cpp +++ b/ecmascript/global_env_constants.cpp @@ -220,6 +220,8 @@ void GlobalEnvConstants::InitRootsClass(JSThread *thread, JSHClass *hClass) factory->NewEcmaReadOnlyHClass(hClass, TSFunctionType::SIZE, JSType::TS_FUNCTION_TYPE)); SetConstant(ConstantIndex::TS_ARRAY_TYPE_CLASS_INDEX, factory->NewEcmaReadOnlyHClass(hClass, TSArrayType::SIZE, JSType::TS_ARRAY_TYPE)); + SetConstant(ConstantIndex::TS_MODULE_NAMESPACE_TYPE_CLASS_INDEX, + factory->NewEcmaReadOnlyHClass(hClass, TSModuleNameSpaceType::SIZE, JSType::TS_MODULE_NAMESPACE_TYPE)); SetConstant(ConstantIndex::TS_ITERATOR_INSTANCE_TYPE_CLASS_INDEX, factory->NewEcmaReadOnlyHClass(hClass, TSIteratorInstanceType::SIZE, JSType::TS_ITERATOR_INSTANCE_TYPE)); diff --git a/ecmascript/global_env_constants.h b/ecmascript/global_env_constants.h index a2d216a70d36025b64053b2d52f5b0bf6a6c5f16..0573cb094eb21f801f0c5b1b5023060451d2caa1 100644 --- a/ecmascript/global_env_constants.h +++ b/ecmascript/global_env_constants.h @@ -88,6 +88,7 @@ class JSThread; V(JSTaggedValue, TSClassInstanceTypeClass, TS_CLASS_INSTANCE_TYPE_CLASS_INDEX, ecma_roots_class) \ V(JSTaggedValue, TSFunctionTypeClass, TS_FUNCTION_TYPE_CLASS_INDEX, ecma_roots_class) \ V(JSTaggedValue, TSArrayTypeClass, TS_ARRAY_TYPE_CLASS_INDEX, ecma_roots_class) \ + V(JSTaggedValue, TSModuleNameSpaceTypeClass, TS_MODULE_NAMESPACE_TYPE_CLASS_INDEX, ecma_roots_class) \ V(JSTaggedValue, TSIteratorInstanceTypeClass, TS_ITERATOR_INSTANCE_TYPE_CLASS_INDEX, ecma_roots_class) \ V(JSTaggedValue, JSSetIteratorClass, JS_SET_ITERATOR_CLASS_INDEX, ecma_roots_class) \ V(JSTaggedValue, JSRegExpIteratorClass, JS_REGEXP_ITERATOR_CLASS_INDEX, ecma_roots_class) \ diff --git a/ecmascript/js_hclass.h b/ecmascript/js_hclass.h index 70d34af1aa43b4e336b9f345c525a8c3f78381d8..b007cfef5a368be2ced879741011a44f1616890a 100644 --- a/ecmascript/js_hclass.h +++ b/ecmascript/js_hclass.h @@ -243,6 +243,7 @@ class PropertyLookupResult; TS_CLASS_TYPE, /* //////////////////////////////////////////////////////////////////////////////-PADDING */ \ TS_CLASS_INSTANCE_TYPE, /* ///////////////////////////////////////////////////////////////////////-PADDING */ \ TS_INTERFACE_TYPE, /* //////////////////////////////////////////////////////////////////////////-PADDING */ \ + TS_MODULE_NAMESPACE_TYPE, /* ///////////////////////////////////////////////////////////////////-PADDING */ \ TS_ITERATOR_INSTANCE_TYPE, /* //////////////////////////////////////////////////////////////////-PADDING */ \ \ VTABLE, /* //////////////////////////////////////////////////////////////////-PADDING */ \ @@ -1332,6 +1333,11 @@ public: return GetObjectType() == JSType::TS_ARRAY_TYPE; } + inline bool IsTSModuleNameSpaceType() const + { + return GetObjectType() == JSType::TS_MODULE_NAMESPACE_TYPE; + } + inline bool IsTSIteratorInstanceType() const { return GetObjectType() == JSType::TS_ITERATOR_INSTANCE_TYPE; diff --git a/ecmascript/js_tagged_value-inl.h b/ecmascript/js_tagged_value-inl.h index a46e26b73af747df71c50aaf96769d9b82e876e4..e4504e9571a73307c4770f4da5ecd882b89c4049 100644 --- a/ecmascript/js_tagged_value-inl.h +++ b/ecmascript/js_tagged_value-inl.h @@ -1319,6 +1319,11 @@ inline bool JSTaggedValue::IsTSIteratorInstanceType() const return IsHeapObject() && GetTaggedObject()->GetClass()->IsTSIteratorInstanceType(); } +inline bool JSTaggedValue::IsTSModuleNameSpaceType() const +{ + return IsHeapObject() && GetTaggedObject()->GetClass()->IsTSModuleNameSpaceType(); +} + inline bool JSTaggedValue::IsModuleRecord() const { return IsHeapObject() && GetTaggedObject()->GetClass()->IsModuleRecord(); diff --git a/ecmascript/js_tagged_value.h b/ecmascript/js_tagged_value.h index db6f6523c1b3cb57db5af1d4356b0ec2100c817e..fcdfb9e05f55c56169a83c6ab91d8f8ab7c80fad 100644 --- a/ecmascript/js_tagged_value.h +++ b/ecmascript/js_tagged_value.h @@ -670,6 +670,7 @@ public: bool IsTSFunctionType() const; bool IsTSArrayType() const; bool IsTSIteratorInstanceType() const; + bool IsTSModuleNameSpaceType() const; bool IsCjsExports() const; bool IsCjsModule() const; diff --git a/ecmascript/mem/object_xray.h b/ecmascript/mem/object_xray.h index 87cbd500b0490aa53ad626d57c3365fb162c2c5c..18eccd2cb7b2c986102c94f94a5871516b5851ed 100644 --- a/ecmascript/mem/object_xray.h +++ b/ecmascript/mem/object_xray.h @@ -499,6 +499,9 @@ public: case JSType::TS_INTERFACE_TYPE: TSInterfaceType::Cast(object)->VisitRangeSlot(visitor); break; + case JSType::TS_MODULE_NAMESPACE_TYPE: + TSModuleNameSpaceType::Cast(object)->VisitRangeSlot(visitor); + break; case JSType::TS_CLASS_INSTANCE_TYPE: break; case JSType::TS_FUNCTION_TYPE: diff --git a/ecmascript/object_factory.cpp b/ecmascript/object_factory.cpp index 0ebf6e9c1090ebdef26fc4367a507bb9b1610aae..e3ccaf52e071d54cb66e47bc31823200108a71ff 100644 --- a/ecmascript/object_factory.cpp +++ b/ecmascript/object_factory.cpp @@ -3357,6 +3357,21 @@ JSHandle ObjectFactory::NewTSIteratorInstanceType() return iteratorInstanceType; } + +JSHandle ObjectFactory::NewTSModuleNameSpaceType(uint32_t length) +{ + NewObjectHook(); + + TaggedObject *header = heap_->AllocateYoungOrHugeObject( + JSHClass::Cast(thread_->GlobalConstants()->GetTSModuleNameSpaceTypeClass().GetTaggedObject())); + JSHandle moduleNameSpaceType(thread_, header); + + moduleNameSpaceType->SetGT(GlobalTSTypeRef::Default()); + moduleNameSpaceType->SetExportArray(thread_, JSTaggedValue::Undefined()); + JSHandle exportArray = NewTaggedArray(length, JSTaggedValue::Undefined()); + moduleNameSpaceType->SetExportArray(thread_, exportArray); + return moduleNameSpaceType; +} // ----------------------------------- new string ---------------------------------------- JSHandle ObjectFactory::NewFromASCII(const CString &data) { diff --git a/ecmascript/object_factory.h b/ecmascript/object_factory.h index 356d61f03c1532602b56e4efc2ab80a0886bfc6f..a3aeea6f92c245a7c69c491e09c9d630d6911eeb 100644 --- a/ecmascript/object_factory.h +++ b/ecmascript/object_factory.h @@ -108,6 +108,7 @@ class TSModuleTable; class TSFunctionType; class TSArrayType; class TSIteratorInstanceType; +class TSModuleNameSpaceType; class JSAPIArrayList; class JSAPIArrayListIterator; class JSAPIDeque; @@ -498,6 +499,7 @@ public: JSHandle NewTSFunctionType(uint32_t length); JSHandle NewTSArrayType(); JSHandle NewTSIteratorInstanceType(); + JSHandle NewTSModuleNameSpaceType(uint32_t length); // ----------------------------------- new string ---------------------------------------- JSHandle NewFromASCII(const CString &data); diff --git a/ecmascript/tests/dump_test.cpp b/ecmascript/tests/dump_test.cpp index 26f6ccde45da190b55d3bf2af48ecbed849d74b5..ca84fc83dab71cf20a96a4b86626a868d5776a8b 100644 --- a/ecmascript/tests/dump_test.cpp +++ b/ecmascript/tests/dump_test.cpp @@ -1029,6 +1029,12 @@ HWTEST_F_L0(EcmaDumpTest, HeapProfileDump) DUMP_FOR_HANDLE(arrayType) break; } + case JSType::TS_MODULE_NAMESPACE_TYPE: { + CHECK_DUMP_FIELDS(TaggedObject::TaggedObjectSize(), TSModuleNameSpaceType::SIZE, 2U); + JSHandle moduleNameSpaceType = factory->NewTSModuleNameSpaceType(1); + DUMP_FOR_HANDLE(moduleNameSpaceType) + break; + } case JSType::TS_ITERATOR_INSTANCE_TYPE: { CHECK_DUMP_FIELDS(TaggedObject::TaggedObjectSize(), TSIteratorInstanceType::SIZE, 2U); JSHandle iteratorInstanceType = factory->NewTSIteratorInstanceType(); diff --git a/ecmascript/ts_types/global_ts_type_ref.h b/ecmascript/ts_types/global_ts_type_ref.h index a785bc414badece3d75bbec1343f58e586ff5bb2..f3c544f3b27dba082435fc85869bc034ebcc19e6 100644 --- a/ecmascript/ts_types/global_ts_type_ref.h +++ b/ecmascript/ts_types/global_ts_type_ref.h @@ -37,6 +37,7 @@ enum class TSTypeKind : uint8_t { // the following typekinds are not recorded in abc files and will be created at compile time ITERATOR_INSTANCE, + MODULE_NAMESPACE, UNKNOWN, TYPEKIND_FIRST = CLASS, diff --git a/ecmascript/ts_types/ts_manager.cpp b/ecmascript/ts_types/ts_manager.cpp index 5ea8f6b0841659cec3438d1a1cd8d0e574725592..b06c1399ec0e3858927227abb9c1c8ce9dc929c2 100644 --- a/ecmascript/ts_types/ts_manager.cpp +++ b/ecmascript/ts_types/ts_manager.cpp @@ -137,6 +137,9 @@ GlobalTSTypeRef TSManager::GetPropType(GlobalTSTypeRef gt, JSHandleIsTSInterfaceType()) { JSHandle objectType(type); return TSInterfaceType::GetPropTypeGT(thread, objectType, propertyName); + } else if (type->IsTSModuleNameSpaceType()) { + JSHandle objectType(type); + return TSModuleNameSpaceType::GetPropTypeGT(thread, objectType, propertyName); } else { LOG_COMPILER(ERROR) << "unsupport TSType GetPropType: " << static_cast(type->GetTaggedObject()->GetClass()->GetObjectType()); @@ -237,6 +240,8 @@ TSTypeKind TSManager::GetTypeKind(const GlobalTSTypeRef >) const return TSTypeKind::INTERFACE; case JSType::TS_ITERATOR_INSTANCE_TYPE: return TSTypeKind::ITERATOR_INSTANCE; + case JSType::TS_MODULE_NAMESPACE_TYPE: + return TSTypeKind::MODULE_NAMESPACE; default: LOG_ECMA(FATAL) << "this branch is unreachable"; UNREACHABLE(); @@ -912,6 +917,8 @@ std::string TSManager::GetTypeStr(kungfu::GateType gateType) const return "interface"; case TSTypeKind::ITERATOR_INSTANCE: return "iterator_instance"; + case TSTypeKind::MODULE_NAMESPACE: + return "module_namespace"; case TSTypeKind::UNKNOWN: return "unknown"; default: diff --git a/ecmascript/ts_types/ts_manager.h b/ecmascript/ts_types/ts_manager.h index 5704d8fd49249bc894f7550678805bcd7b5fa5e8..9bf6a3bbf99aad5e86818575476cb0033b5b7756 100644 --- a/ecmascript/ts_types/ts_manager.h +++ b/ecmascript/ts_types/ts_manager.h @@ -414,6 +414,7 @@ public: V(Import, TSTypeKind::IMPORT) \ V(Interface, TSTypeKind::INTERFACE) \ V(IteratorInstance, TSTypeKind::ITERATOR_INSTANCE) \ + V(ModuleNameSpace, TSTypeKind::MODULE_NAMESPACE) \ #define IS_TSTYPEKIND(NAME, TSTYPEKIND) \ inline bool PUBLIC_API Is##NAME##TypeKind(const kungfu::GateType &gateType) const \ diff --git a/ecmascript/ts_types/ts_type.cpp b/ecmascript/ts_types/ts_type.cpp index 80fde232e75d6bb5ffe135833d7c35d1a28d3d78..484e83389c86a763b5feb45a8d71570779c412f2 100644 --- a/ecmascript/ts_types/ts_type.cpp +++ b/ecmascript/ts_types/ts_type.cpp @@ -389,4 +389,45 @@ GlobalTSTypeRef TSInterfaceType::GetPropTypeGT(JSThread *thread, JSHandle moduleNameSpaceType, + JSHandle propName) +{ + DISALLOW_GARBAGE_COLLECTION; + GlobalTSTypeRef propTypeGT = GlobalTSTypeRef::Default(); + JSHandle exportArray(thread, moduleNameSpaceType->GetExportArray()); + uint32_t len = exportArray->GetLength(); + JSMutableHandle exportTable(thread, JSTaggedValue::Undefined()); + for(uint32_t index = 0; index < len; index++) { + JSTaggedValue tempTable = exportArray->Get(index); + if (!tempTable.IsTaggedArray()) { + continue; + } + exportTable.Update(tempTable); + propTypeGT = SearchByName(thread, propName, exportTable); + if (!propTypeGT.IsDefault()) { + return propTypeGT; + } + } + return propTypeGT; +} + +GlobalTSTypeRef TSModuleNameSpaceType::SearchByName(JSThread *thread, JSHandle target, + JSHandle &exportTable) +{ + DISALLOW_GARBAGE_COLLECTION; + uint32_t length = exportTable->GetLength(); + JSMutableHandle propKey(thread, JSTaggedValue::Undefined()); + // the exportTable is arranged as follows ["A", "101", "B", "102"] + // get GT of a export type specified by its descriptor/name + for (uint32_t i = 0; i < length; i = i + 2) { // 2: symbol and symbolType + propKey.Update(exportTable->Get(i)); + if (JSTaggedValue::Equal(thread, target, propKey)) { + // Transform raw data of JSTaggedValue to GT + return GlobalTSTypeRef(exportTable->Get(i + 1).GetInt()); + } + } + return GlobalTSTypeRef::Default(); +} } // namespace panda::ecmascript diff --git a/ecmascript/ts_types/ts_type.h b/ecmascript/ts_types/ts_type.h index 18624cc533e2e8c18cf1dc0e54709c804728e458..ad4101c5e512f24b6d9fb26d227b518071774324 100644 --- a/ecmascript/ts_types/ts_type.h +++ b/ecmascript/ts_types/ts_type.h @@ -222,6 +222,24 @@ public: DECL_DUMP() }; + +class TSModuleNameSpaceType : public TSType { +public: + CAST_CHECK(TSModuleNameSpaceType, IsTSModuleNameSpaceType); + + static constexpr size_t EXPORT_ARRAY_OFFSET = TSType::SIZE; + + static GlobalTSTypeRef GetPropTypeGT(JSThread *thread, JSHandle moduleNameSpaceType, + JSHandle propName); + + ACCESSORS(ExportArray, EXPORT_ARRAY_OFFSET, SIZE); + + DECL_VISIT_OBJECT(EXPORT_ARRAY_OFFSET, SIZE) + DECL_DUMP() +private: + static GlobalTSTypeRef SearchByName(JSThread *thread, JSHandle target, + JSHandle &exportTable); +}; } // namespace panda::ecmascript #endif // ECMASCRIPT_TS_TYPES_TS_TYPE_H diff --git a/ecmascript/ts_types/ts_type_parser.cpp b/ecmascript/ts_types/ts_type_parser.cpp index 70f2d7560aa67a1e4bb12c20a793bd0775a95fba..8ad818078573f7db53e6b2df106553b9c17e5c98 100644 --- a/ecmascript/ts_types/ts_type_parser.cpp +++ b/ecmascript/ts_types/ts_type_parser.cpp @@ -147,12 +147,78 @@ GlobalTSTypeRef TSTypeParser::ResolveImportType(const JSPandaFile *jsPandaFile, JSHandle table = tableGenerator_.GetOrGenerateTSTypeTable(jsPandaFile, entryPoint, moduleId); JSHandle exportTable = GenerateExportTableFromRecord(jsPandaFile, entryPoint, table); JSHandle arrayWithGT(exportTable); - JSHandle targetVarName = GenerateImportVar(importVarNamePath); + JSHandle targetVarName; + bool isModuleNameSpace = false; + std::tie(targetVarName, isModuleNameSpace)= GenerateImportVar(importVarNamePath); std::unordered_set markSet; + if (isModuleNameSpace) { + uint32_t importModuleId = tableGenerator_.TryGetModuleId(recordName); + if (UNLIKELY(!GlobalTSTypeRef::IsVaildModuleId(moduleId))) { + LOG_COMPILER(DEBUG) << "The maximum number of TSTypeTables is reached. All TSTypes in the record " + << recordName << " will not be parsed and will be treated as any."; + return GetAndStoreGT(jsPandaFile, typeId, recordName); + } + + JSHandle curTable = tableGenerator_.GetOrGenerateTSTypeTable(jsPandaFile, + recordName, importModuleId); + uint32_t importLocalId = tableGenerator_.TryGetLocalId(curTable); + if (UNLIKELY(!GlobalTSTypeRef::IsVaildLocalId(importLocalId))) { + LOG_COMPILER(DEBUG) << "The maximum number of TSTypes in TSTypeTable " << importLocalId << " is reached. " + << "The TSType with typeId " << typeId << " in the record " << recordName + << " will not be parsed and will be treated as any."; + return GetAndStoreGT(jsPandaFile, typeId, recordName); + } + GlobalTSTypeRef gt = GetAndStoreGT(jsPandaFile, typeId, recordName, importModuleId, importLocalId); + JSHandle moduleNameSpace = ParseModuleNameSpace(exportTable, + jsPandaFile, recordName, markSet); + SetTSType(curTable, moduleNameSpace, gt); + return gt; + } GlobalTSTypeRef importedGT = GetExportGTByName(targetVarName, arrayWithGT, jsPandaFile, entryPoint, markSet); return GetAndStoreImportGT(jsPandaFile, typeId, recordName, importedGT); } +JSHandle TSTypeParser::ParseModuleNameSpace(JSHandle exportTable, + const JSPandaFile *jsPandaFile, + const CString &recordName, + std::unordered_set &markSet) +{ + std::vector> exportTables; + exportTables.emplace_back(exportTable); + IterateStarExportTable(jsPandaFile, recordName, exportTables, markSet); + auto len = exportTables.size(); + JSHandle nmType = factory_->NewTSModuleNameSpaceType(len); + JSHandle exportArray(thread_, nmType->GetExportArray()); + for (auto index = 0; index < len; index++) { + exportArray->Set(thread_, index, exportTables[index].GetTaggedValue()); + } + return JSHandle(nmType); +} + +void TSTypeParser::IterateStarExportTable(const JSPandaFile *jsPandaFile, + const CString &recordName, + std::vector> &exportTables, + std::unordered_set &markSet) +{ + if (bcInfo_->HasStarExportToRecord(recordName)) { + const auto &starRecords = bcInfo_->GetstarExportToRecord(recordName); + markSet.insert(recordName); + for (const auto &star : starRecords) { + // use markSet to avoid circular import + if (markSet.find(star) != markSet.end()) { + continue; + } + uint32_t starModuleId = tableGenerator_.TryGetModuleId(star); + if (UNLIKELY(!GlobalTSTypeRef::IsVaildModuleId(starModuleId))) { + continue; + } + JSHandle table = tableGenerator_.GetOrGenerateTSTypeTable(jsPandaFile, star, starModuleId); + exportTables.emplace_back(GenerateExportTableFromRecord(jsPandaFile, star, table)); + IterateStarExportTable(jsPandaFile, star, exportTables, markSet); + } + } +} + JSHandle TSTypeParser::ParseNonImportType(const JSPandaFile *jsPandaFile, const CString &recordName, TypeLiteralExtractor *typeLiteralExtractor) { @@ -490,14 +556,14 @@ JSHandle TSTypeParser::GenerateImportRelativePath(JSHandleNewFromUtf8(path); // #A#./A -> ./A } -JSHandle TSTypeParser::GenerateImportVar(JSHandle import) const +std::pair, bool> TSTypeParser::GenerateImportVar(JSHandle import) const { // importNamePath #A#./A CString importVarNamePath = ConvertToString(import.GetTaggedValue()); auto firstPos = importVarNamePath.find_first_of('#'); auto lastPos = importVarNamePath.find_last_of('#'); CString target = importVarNamePath.substr(firstPos + 1, lastPos - firstPos - 1); - return factory_->NewFromUtf8(target); // #A#./A -> A + return std::pair(factory_->NewFromUtf8(target), target == "*"); // #A#./A -> A } GlobalTSTypeRef TSTypeParser::GetExportGTByName(JSHandle target, JSHandle &exportTable, diff --git a/ecmascript/ts_types/ts_type_parser.h b/ecmascript/ts_types/ts_type_parser.h index bb66765434a15cc1371a193a3d8513346629546b..3932af486bb99b4a49b42bbb566080a39deab128 100644 --- a/ecmascript/ts_types/ts_type_parser.h +++ b/ecmascript/ts_types/ts_type_parser.h @@ -69,6 +69,11 @@ private: GlobalTSTypeRef ParseBuiltinObjType(uint32_t typeId); + JSHandle ParseModuleNameSpace(JSHandle exportTable, + const JSPandaFile *jsPandaFile, + const CString &recordName, + std::unordered_set &markSet); + GlobalTSTypeRef ResolveImportType(const JSPandaFile *jsPandaFile, const CString &recordName, TypeLiteralExtractor *typeLiteralExtractor); @@ -130,7 +135,7 @@ private: JSHandle GenerateImportRelativePath(JSHandle importRel) const; - JSHandle GenerateImportVar(JSHandle import) const; + std::pair, bool> GenerateImportVar(JSHandle import) const; GlobalTSTypeRef GetExportGTByName(JSHandle target, JSHandle &exportTable, const JSPandaFile *jsPandaFile, const CString &recordName, @@ -139,6 +144,11 @@ private: GlobalTSTypeRef IterateStarExport(JSHandle target, const JSPandaFile *jsPandaFile, const CString &recordName, std::unordered_set &markSet); + void IterateStarExportTable(const JSPandaFile *jsPandaFile, + const CString &recordName, + std::vector> &exportTables, + std::unordered_set &markSet); + TSManager *tsManager_ {nullptr}; EcmaVM *vm_ {nullptr}; JSThread *thread_ {nullptr}; diff --git a/test/typeinfer/module_test/module_namespace/BUILD.gn b/test/typeinfer/module_test/module_namespace/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..2c4a19942dfe7e4a07663e65655f16895e390eaa --- /dev/null +++ b/test/typeinfer/module_test/module_namespace/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//arkcompiler/ets_runtime/test/test_helper.gni") + +host_typeinfer_test_action("module_namespace") { + deps = [] + is_multi_file_tests = true + is_enable_es2abc = true +} diff --git a/test/typeinfer/module_test/module_namespace/export.ts b/test/typeinfer/module_test/module_namespace/export.ts new file mode 100644 index 0000000000000000000000000000000000000000..e39315a95975b8d610e8cb423552ef0c0875e221 --- /dev/null +++ b/test/typeinfer/module_test/module_namespace/export.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare function AssertType(value:any, type:string):void; +export class Test1 { + value1:string; + constructor(value1:string) { + this.value1 = value1; + } + foo(): string { + return this.value1; + } +} + +let t1 = new Test1("abc"); +AssertType(t1, "Test1"); +AssertType(t1.value1, "string"); +AssertType(t1.foo(), "string"); + + +export let test2 : string = "efg"; diff --git a/test/typeinfer/module_test/module_namespace/module_namespace.ts b/test/typeinfer/module_test/module_namespace/module_namespace.ts new file mode 100644 index 0000000000000000000000000000000000000000..043e02c51d384a4ba8771c446e9e0b4f9a03bb99 --- /dev/null +++ b/test/typeinfer/module_test/module_namespace/module_namespace.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare function AssertType(value:any, type:string):void; + +import * as nm from "./export"; + +let test1 = new nm.Test1("efg") +AssertType(test1, "Test1"); +AssertType(test1.value1, "string"); +AssertType(test1.foo(), "string"); + +AssertType(nm.test2, "string"); \ No newline at end of file diff --git a/test/typeinfer/module_test/module_namespace_retransmission/BUILD.gn b/test/typeinfer/module_test/module_namespace_retransmission/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..955251a6ff94620d58b8a6602e0eebe95a9ec2d0 --- /dev/null +++ b/test/typeinfer/module_test/module_namespace_retransmission/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//arkcompiler/ets_runtime/test/test_helper.gni") + +host_typeinfer_test_action("module_namespace_retransmission") { + deps = [] + is_multi_file_tests = true + is_enable_es2abc = true +} diff --git a/test/typeinfer/module_test/module_namespace_retransmission/export1.ts b/test/typeinfer/module_test/module_namespace_retransmission/export1.ts new file mode 100644 index 0000000000000000000000000000000000000000..36d291d8a4f8065d3f9d6f7b4b3dfb0b4342c994 --- /dev/null +++ b/test/typeinfer/module_test/module_namespace_retransmission/export1.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare function AssertType(value:any, type:string):void; +export * from "./export2" +export class Test2 { + value2:number; + constructor(value2:number) { + this.value2 = value2; + } + poo(): number { + return this.value2 / 2; + } +} + +let t2 = new Test2(10); +AssertType(t2, "Test2"); +AssertType(t2.value2, "number"); +AssertType(t2.poo(), "number"); \ No newline at end of file diff --git a/test/typeinfer/module_test/module_namespace_retransmission/export2.ts b/test/typeinfer/module_test/module_namespace_retransmission/export2.ts new file mode 100644 index 0000000000000000000000000000000000000000..7962f959668811855fc35f76d2da726e0d8dc264 --- /dev/null +++ b/test/typeinfer/module_test/module_namespace_retransmission/export2.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare function AssertType(value:any, type:string):void; +export class Test1 { + value1:string; + constructor(value1:string) { + this.value1 = value1; + } + foo(): string { + return this.value1; + } +} + +let t1 = new Test1("abc"); +AssertType(t1, "Test1"); +AssertType(t1.value1, "string"); +AssertType(t1.foo(), "string"); + + +export let test2 : string = "efg"; \ No newline at end of file diff --git a/test/typeinfer/module_test/module_namespace_retransmission/module_namespace_retransmission.ts b/test/typeinfer/module_test/module_namespace_retransmission/module_namespace_retransmission.ts new file mode 100644 index 0000000000000000000000000000000000000000..67160b377a2a40923dfb58d8dd21946c0afb6779 --- /dev/null +++ b/test/typeinfer/module_test/module_namespace_retransmission/module_namespace_retransmission.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare function AssertType(value:any, type:string):void; + +import * as nm from "./export1"; + +let test1 = new nm.Test1("efg") +AssertType(test1, "Test1"); +AssertType(test1.value1, "string"); +AssertType(test1.foo(), "string"); + +AssertType(nm.test2, "string"); + +let test3 = new nm.Test2(5); +AssertType(test3, "Test2"); +AssertType(test3.value2, "number"); +AssertType(test3.poo(), "number"); \ No newline at end of file