diff --git a/ecmascript/module/js_module_source_text.cpp b/ecmascript/module/js_module_source_text.cpp index aa05d29bb1..45c07236a3 100644 --- a/ecmascript/module/js_module_source_text.cpp +++ b/ecmascript/module/js_module_source_text.cpp @@ -353,9 +353,9 @@ void SourceTextModule::MakeNormalizedAppArgs(const EcmaVM *vm, std::vector> &arguments, - const CString &soPath, const CString &moduleName) + const CString &soPath, const CString &moduleName, const CString &requestName) { - if (vm->IsNormalizedOhmUrlPack()) { + if (!StringHelper::StringStartWith(requestName, ModulePathHelper::REQUIRE_NAPI_APP_PREFIX)) { return MakeNormalizedAppArgs(vm, arguments, soPath, moduleName); } size_t pos = soPath.find_last_of(PathHelper::SLASH_TAG); @@ -402,7 +402,7 @@ Local SourceTextModule::LoadNativeModuleImpl(EcmaVM *vm, JSThread *t arguments.emplace_back(StringRef::NewFromUtf8(vm, soName.c_str())); if (moduleType == ModuleTypes::APP_MODULE) { - MakeAppArgs(vm, arguments, soName, moduleName); + MakeAppArgs(vm, arguments, soName, moduleName, moduleRequestName); } else if (moduleType == ModuleTypes::INTERNAL_MODULE) { MakeInternalArgs(vm, arguments, moduleRequestName); } diff --git a/ecmascript/module/js_module_source_text.h b/ecmascript/module/js_module_source_text.h index 7f5339d12f..f8ac6911ea 100644 --- a/ecmascript/module/js_module_source_text.h +++ b/ecmascript/module/js_module_source_text.h @@ -166,7 +166,7 @@ public: static void MakeNormalizedAppArgs(const EcmaVM *vm, std::vector> &arguments, const CString &soPath, const CString &moduleName); static void MakeAppArgs(const EcmaVM *vm, std::vector> &arguments, - const CString &soPath, const CString &moduleName); + const CString &soPath, const CString &moduleName, const CString &requestName); static void MakeInternalArgs(const EcmaVM *vm, std::vector> &arguments, const CString &moduleRequestName); static Local LoadNativeModuleImpl(EcmaVM *vm, JSThread *thread, diff --git a/ecmascript/module/module_path_helper.cpp b/ecmascript/module/module_path_helper.cpp index 8cfceef82a..739eb26868 100644 --- a/ecmascript/module/module_path_helper.cpp +++ b/ecmascript/module/module_path_helper.cpp @@ -63,7 +63,8 @@ CString ModulePathHelper::ConcatMergeFileNameToNormalized(JSThread *thread, cons } else if (IsImportFile(requestName)) { // this branch save for import "xxx.js" in npm CString inputPath = requestName; - CString entryPoint = ConcatImportFileNormalizedOhmurlWithRecordName(jsPandaFile, recordName, requestName); + CString entryPoint = ConcatImportFileNormalizedOhmurlWithRecordName(thread, jsPandaFile, baseFileName, + recordName, requestName); if (entryPoint.empty()) { THROW_MODULE_NOT_FOUND_ERROR_WITH_RETURN_VALUE(thread, inputPath, recordName, requestName); } @@ -79,35 +80,44 @@ CString ModulePathHelper::ConcatMergeFileNameToNormalized(JSThread *thread, cons * Before: requestName: ../xxx1/xxx2 || ./xxx1 * After: &entryPath&version */ -CString ModulePathHelper::ConcatImportFileNormalizedOhmurlWithRecordName(const JSPandaFile *jsPandaFile, - const CString &recordName, CString &requestName) +CString ModulePathHelper::ConcatImportFileNormalizedOhmurlWithRecordName(JSThread *thread, + const JSPandaFile *jsPandaFile, CString &baseFileName, const CString &recordName, const CString &requestName) { CString entryPoint; CVector res = SplitNormalizedRecordName(recordName); CString path = PathHelper::NORMALIZED_OHMURL_TAG + res[NORMALIZED_IMPORT_PATH_INDEX]; CString version = res[NORMALIZED_VERSION_INDEX]; - requestName = RemoveSuffix(requestName); - size_t pos = requestName.find(PathHelper::CURRENT_DIREATORY_TAG); + CString moduleRequestName = requestName; + moduleRequestName = RemoveSuffix(moduleRequestName); + size_t pos = moduleRequestName.find(PathHelper::CURRENT_DIREATORY_TAG); if (pos == 0) { - requestName = requestName.substr(CURRENT_DIREATORY_TAG_LEN); + moduleRequestName = moduleRequestName.substr(CURRENT_DIREATORY_TAG_LEN); } pos = path.rfind(PathHelper::SLASH_TAG); if (pos != CString::npos) { - entryPoint = path.substr(0, pos + 1) + requestName; + entryPoint = path.substr(0, pos + 1) + moduleRequestName; } else { - entryPoint = requestName; + entryPoint = moduleRequestName; } entryPoint = PathHelper::NormalizePath(entryPoint); - requestName = ConcatImportFileNormalizedOhmurl(entryPoint, "", version); - if (jsPandaFile->HasRecord(requestName)) { - return requestName; + moduleRequestName = ConcatImportFileNormalizedOhmurl(entryPoint, "", version); + if (jsPandaFile->HasRecord(moduleRequestName)) { + return moduleRequestName; } // requestName may be a folder entryPoint += PACKAGE_ENTRY_FILE; entryPoint = PathHelper::NormalizePath(entryPoint); - requestName = ConcatImportFileNormalizedOhmurl(entryPoint, "", version); - if (jsPandaFile->HasRecord(requestName)) { - return requestName; + moduleRequestName = ConcatImportFileNormalizedOhmurl(entryPoint, "", version); + if (jsPandaFile->HasRecord(moduleRequestName)) { + return moduleRequestName; + } + + // requestName may be a packageName + moduleRequestName = requestName; + ConcatOtherNormalizedOhmurl(thread->GetEcmaVM(), jsPandaFile, baseFileName, moduleRequestName); + moduleRequestName = ParseNormalizedOhmUrl(thread, baseFileName, recordName, moduleRequestName); + if (jsPandaFile->HasRecord(moduleRequestName)) { + return moduleRequestName; } return CString(); } @@ -896,7 +906,8 @@ CString ModulePathHelper::TranslateExpressionToNormalized(JSThread *thread, cons EcmaVM *vm = thread->GetEcmaVM(); CString inputPath = requestPath; if (IsImportFile(requestPath)) { - CString entryPoint = ConcatImportFileNormalizedOhmurlWithRecordName(jsPandaFile, recordName, requestPath); + CString entryPoint = ConcatImportFileNormalizedOhmurlWithRecordName(thread, jsPandaFile, baseFileName, + recordName, requestPath); if (entryPoint.empty()) { THROW_MODULE_NOT_FOUND_ERROR_WITH_RETURN_VALUE(thread, inputPath, recordName, requestPath); } diff --git a/ecmascript/module/module_path_helper.h b/ecmascript/module/module_path_helper.h index c467326fc3..0875bf84ed 100644 --- a/ecmascript/module/module_path_helper.h +++ b/ecmascript/module/module_path_helper.h @@ -189,8 +189,9 @@ public: CString &baseFileName, const CString &recordName, CString requestName); static CVector SplitNormalizedRecordName(const CString &recordName); - static CString ConcatImportFileNormalizedOhmurlWithRecordName(const JSPandaFile *jsPandaFile, - const CString &recordName, CString &requestName); + static CString ConcatImportFileNormalizedOhmurlWithRecordName(JSThread *thread, const JSPandaFile *jsPandaFile, + CString &baseFileName, const CString &recordName, + const CString &requestName); static void ConcatOtherNormalizedOhmurl(EcmaVM *vm, const JSPandaFile *jsPandaFile, [[maybe_unused]] CString &baseFileName, CString &requestPath); static CString ConcatNormalizedOhmurlWithData(CVector &data, CString &pkgName, CString &entryPath); diff --git a/ecmascript/module/tests/ecma_module_test.cpp b/ecmascript/module/tests/ecma_module_test.cpp index 39e2cb6be4..c297987543 100644 --- a/ecmascript/module/tests/ecma_module_test.cpp +++ b/ecmascript/module/tests/ecma_module_test.cpp @@ -819,4 +819,35 @@ HWTEST_F_L0(EcmaModuleTest, ConcatPreviewTestUnifiedOhmUrl) CString res = ModulePathHelper::ConcatPreviewTestUnifiedOhmUrl(bundleName, pkgName, path, version); EXPECT_EQ(res, exceptOutUrl); } + +HWTEST_F_L0(EcmaModuleTest, NeedTranslateToNormalized) +{ + CString requestName = "@ohos:hilog"; + bool res = ModulePathHelper::NeedTranslateToNormalized(requestName); + EXPECT_EQ(res, false); + + requestName = "@app:com.example.myapplication/entry"; + res = ModulePathHelper::NeedTranslateToNormalized(requestName); + EXPECT_EQ(res, false); + + requestName = "@bundle:com.example.myapplication/library"; + res = ModulePathHelper::NeedTranslateToNormalized(requestName); + EXPECT_EQ(res, false); + + requestName = "@package:pkg_modules/.ohpm/json5@2.2.3/pkg_modules/json5/dist/index"; + res = ModulePathHelper::NeedTranslateToNormalized(requestName); + EXPECT_EQ(res, false); + + requestName = "@normalized:N&&&har/Index&1.0.0"; + res = ModulePathHelper::NeedTranslateToNormalized(requestName); + EXPECT_EQ(res, false); + + requestName = "json5"; + res = ModulePathHelper::NeedTranslateToNormalized(requestName); + EXPECT_EQ(res, true); + + requestName = "library"; + res = ModulePathHelper::NeedTranslateToNormalized(requestName); + EXPECT_EQ(res, true); +} } // namespace panda::test