1 /* 2 * Copyright (c) 2024 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 #ifndef ECMASCRIPT_MODULE_MODULE_PATH_HELPER_H 16 #define ECMASCRIPT_MODULE_MODULE_PATH_HELPER_H 17 18 #include "ecmascript/base/path_helper.h" 19 20 #include "ecmascript/compiler/aot_file/aot_file_manager.h" 21 #include "ecmascript/base/string_helper.h" 22 #include "ecmascript/ecma_macros.h" 23 #include "ecmascript/ecma_string.h" 24 #include "ecmascript/ecma_vm.h" 25 #include "ecmascript/global_env.h" 26 #include "ecmascript/js_tagged_value-inl.h" 27 #include "ecmascript/jspandafile/js_pandafile.h" 28 /* 29 * Intra-application cross hap: 30 * baseFileName = 'data/storage/el1/bundle/moduleName/ets/modules.abc'; 31 * cross-application: 32 * baseFileName = 'data/storage/el1/bundle/bundleName/moduleName/moduleName/ets/modules.abc'; 33 * recordName = bundleName/moduleName/xxx(entry)/xxx(ets)/xxx(pages)/xxx specific abc file 34 * 35 * ohmUrl: It's an index information that can uniquely identify module files. 36 * Current ohmUrl has the following five different prefixs: 37 * 1. @bundle:... Identify OpenHarmony modules. 38 * {project_path}\entry\src\main\ets\pages\Index --> @bundle:bundleName/moduleName/ets/pages/Index 39 * @namespace: needs to add when import local har or ohosTest import entry file. 40 * {project_path}\namespace\src\main\ets\pages\Index --> @bundle:bundleName/moduleName@namespace/ets/pages/Index 41 * 42 * 2. @package:... Identify open source third party modules. 43 * {project_path}\node_modules.ohpm\pkgName\oh_modules\pkgName\xxx\xxx 44 * --> @package:pkg_modules/.ohpm/pkgName/pkg_modules/pkgName/xxx/xxx 45 * 46 * 3. @app:... Identify c++ modules in application. 47 * libxxx.so --> @app:bundleName/moduleName/xxx 48 * 49 * 4. @native:... Identify system builtin modules. 50 * system.app --> @native:system.app 51 * 52 * 5. @ohos:... Identify ohos builtin modules. 53 * @ohos:hilog --> @ohos:hilog 54 */ 55 56 namespace panda::ecmascript { 57 using PathHelper = base::PathHelper; 58 using StringHelper = base::StringHelper; 59 60 enum ValidateFilePath { ABC, ETS_MODULES }; 61 62 class ModulePathHelper { 63 public: 64 static constexpr char EXT_NAME_ABC[] = ".abc"; 65 static constexpr char EXT_NAME_ETS[] = ".ets"; 66 static constexpr char EXT_NAME_TS[] = ".ts"; 67 static constexpr char EXT_NAME_JS[] = ".js"; 68 static constexpr char EXT_NAME_JSON[] = ".json"; 69 static constexpr char EXT_NAME_Z_SO[] = ".z.so"; 70 static constexpr char EXT_NAME_D_TS[] = ".d.ts"; 71 static constexpr char EXT_NAME_MJS[] = ".mjs"; 72 static constexpr char EXT_NAME_HQF[] = ".hqf"; 73 static constexpr char PREFIX_NORMALIZED[] = "@normalized:"; 74 static constexpr char PREFIX_NORMALIZED_SO[] = "@normalized:Y"; 75 static constexpr char PREFIX_NORMALIZED_NOT_SO[] = "@normalized:N"; 76 static constexpr char PREFIX_BUNDLE[] = "@bundle:"; 77 static constexpr char PREFIX_MODULE[] = "@module:"; 78 static constexpr char PREFIX_PACKAGE[] = "@package:"; 79 static constexpr char PREFIX_ETS[] = "ets/"; 80 static constexpr char PREFIX_LIB[] = "lib"; 81 static constexpr char REQUIRE_NAITVE_MODULE_PREFIX[] = "@native:"; 82 static constexpr char REQUIRE_NAPI_OHOS_PREFIX[] = "@ohos:"; 83 static constexpr char REQUIRE_NAPI_APP_PREFIX[] = "@app:"; 84 static constexpr char RAW_ARKUIX_PREFIX[] = "@arkui-x."; 85 static constexpr char NPM_PATH_SEGMENT[] = "node_modules"; 86 static constexpr char PACKAGE_PATH_SEGMENT[] = "pkg_modules"; 87 static constexpr char PACKAGE_ENTRY_FILE[] = "/index"; 88 static constexpr char BUNDLE_INSTALL_PATH[] = "/data/storage/el1/bundle/"; 89 static constexpr char MERGE_ABC_ETS_MODULES[] = "/ets/modules.abc"; 90 static constexpr char ABC[] = ".abc"; 91 static constexpr char MODULE_DEFAULE_ETS[] = "/ets/"; 92 static constexpr char BUNDLE_SUB_INSTALL_PATH[] = "/data/storage/el1/"; 93 static constexpr char PREVIEW_OF_ACROSS_HAP_FLAG[] = "[preview]"; 94 static constexpr char PREVIER_TEST_DIR[] = ".test"; 95 static constexpr char PHYCICAL_FILE_PATH[] = "/src/main"; 96 static constexpr char VMA_NAME_ARKTS_CODE[] = "ArkTS Code"; 97 static constexpr char ENTRY_MAIN_FUNCTION[] = "_GLOBAL::func_main_0"; 98 static constexpr char TRUE_FLAG[] = "true"; 99 100 static constexpr size_t MAX_PACKAGE_LEVEL = 1; 101 static constexpr size_t SEGMENTS_LIMIT_TWO = 2; 102 static constexpr size_t EXT_NAME_ABC_LEN = 4; 103 static constexpr size_t EXT_NAME_ETS_LEN = 4; 104 static constexpr size_t EXT_NAME_TS_LEN = 3; 105 static constexpr size_t EXT_NAME_JS_LEN = 3; 106 static constexpr size_t EXT_NAME_JSON_LEN = 5; 107 static constexpr size_t PREFIX_BUNDLE_LEN = 8; 108 static constexpr size_t PREFIX_MODULE_LEN = 8; 109 static constexpr size_t PREFIX_PACKAGE_LEN = 9; 110 static constexpr size_t NATIVE_PREFIX_SIZE = 8; 111 static constexpr size_t OHOS_PREFIX_SIZE = 6; 112 static constexpr size_t APP_PREFIX_SIZE = 5; 113 static constexpr size_t BUNDLE_INSTALL_PATH_LEN = 25; 114 static constexpr size_t PHYCICAL_FILE_PATH_LEN = 10; 115 static constexpr size_t NORMALIZED_OHMURL_ARGS_NUM = 5; 116 static constexpr size_t NORMALIZED_MODULE_NAME_INDEX = 1; 117 static constexpr size_t NORMALIZED_BUNDLE_NAME_INDEX = 2; 118 static constexpr size_t NORMALIZED_IMPORT_PATH_INDEX = 3; 119 static constexpr size_t NORMALIZED_VERSION_INDEX = 4; 120 static constexpr size_t CURRENT_DIREATORY_TAG_LEN = 2; 121 static constexpr size_t SO_PREFIX_LEN = 3; 122 static constexpr size_t SO_SUFFIX_LEN = 3; 123 124 static constexpr size_t PKGINFO_PACKAGE_NAME_INDEX = 1; 125 static constexpr size_t PKGINFO_BUDNLE_NAME_INDEX = 3; 126 static constexpr size_t PKGINFO_MODULE_NAME_INDEX = 5; 127 static constexpr size_t PKGINFO_VERSION_INDEX = 7; 128 static constexpr size_t PKGINFO_ENTRY_PATH_INDEX = 9; 129 static constexpr size_t PKGINFO_IS_SO_INDEX = 11; 130 131 static CString PUBLIC_API ConcatFileNameWithMerge(JSThread *thread, const JSPandaFile *jsPandaFile, 132 CString &baseFileName, CString recordName, CString requestName); 133 static void ParseAbcPathAndOhmUrl(EcmaVM *vm, const CString &inputFileName, CString &outBaseFileName, 134 CString &outEntryPoint); 135 static CString ConcatUnifiedOhmUrl(const CString &bundleName, const CString &pkgname, const CString &entryPath, 136 const CString &path, const CString &version); 137 static CString ConcatUnifiedOhmUrl(const CString &bundleName, const CString &normalizedpath, 138 const CString &version); 139 static CString ConcatPreviewTestUnifiedOhmUrl(const CString &bundleName, const CString &pkgname, 140 const CString &path, const CString &version); 141 static CString ConcatHspFileNameCrossBundle(const CString &bundleName, const CString &moduleName); 142 static CString ConcatHspFileName(const CString &moduleName); 143 static CString TransformToNormalizedOhmUrl(EcmaVM *vm, const CString &inputFileName, const CString &baseFileName, 144 const CString &oldEntryPoint); 145 static CString ParseUrl(EcmaVM *vm, const CString &recordName); 146 static CString ParsePrefixBundle(JSThread *thread, const JSPandaFile *jsPandaFile, 147 [[maybe_unused]] CString &baseFileName, CString moduleRequestName, [[maybe_unused]] CString recordName); 148 static CString ParseNormalizedOhmUrl(JSThread *thread, CString &baseFileName, const CString &recordName, 149 CString requestName); 150 static CString MakeNewRecord(JSThread *thread, const JSPandaFile *jsPandaFile, CString &baseFileName, 151 const CString &recordName, const CString &requestName); 152 static CString FindOhpmEntryPoint(const JSPandaFile *jsPandaFile, const CString &ohpmPath, 153 const CString &requestName); 154 static CString FindPackageInTopLevelWithNamespace(const JSPandaFile *jsPandaFile, const CString &requestName, 155 const CString &recordName); 156 static CString ParseOhpmPackage(const JSPandaFile *jsPandaFile, const CString &recordName, 157 const CString &requestName); 158 static CString ParseThirdPartyPackage(const JSPandaFile *jsPandaFile, const CString &recordName, 159 const CString &requestName, const CString &packagePath); 160 static CString ParseThirdPartyPackage(const JSPandaFile *jsPandaFile, const CString &recordName, 161 const CString &requestName); 162 static void ResolveCurrentPath(CString &dirPath, CString &fileName, const JSPandaFile *jsPandaFile); 163 static CString FindNpmEntryPoint(const JSPandaFile *jsPandaFile, const CString &packageEntryPoint); 164 static CString FindPackageInTopLevel(const JSPandaFile *jsPandaFile, const CString &requestName, 165 const CString &packagePath); 166 static bool IsImportFile(const CString &moduleRequestName); 167 static CString RemoveSuffix(const CString &requestName); 168 static bool NeedTranstale(const CString &requestName); 169 static bool NeedTranslateToNormalized(const CString &requestName); 170 static void TranstaleExpressionInput(const JSPandaFile *jsPandaFile, CString &requestPath); 171 static CString GetModuleNameWithBaseFile(const CString &baseFileName); 172 static CString TranslateExpressionInputWithEts(JSThread *thread, const JSPandaFile *jsPandaFile, 173 CString &baseFileName, const CString &requestName); 174 static void ParseCrossModuleFile(const JSPandaFile *jsPandaFile, CString &requestPath); 175 static CString ReformatPath(CString requestName); 176 static CString TranslateExpressionToNormalized(JSThread *thread, const JSPandaFile *jsPandaFile, 177 [[maybe_unused]] CString &baseFileName, const CString &recordName, 178 CString &requestPath); 179 static CVector<CString> GetPkgContextInfoListElements(EcmaVM *vm, CString &moduleName, 180 CString &packageName); 181 static CString TranslateNapiFileRequestPath(JSThread *thread, const CString &modulePath, 182 const CString &requestName); 183 static CVector<CString> SplitNormalizedOhmurl(const CString &ohmurl); 184 static CString ConcatImportFileNormalizedOhmurl(const CString &recordPath, const CString &requestName, 185 const CString &version = ""); 186 static CString ConcatNativeSoNormalizedOhmurl(const CString &moduleName, const CString &bundleName, 187 const CString &pkgName, const CString &version); 188 static CString ConcatNotSoNormalizedOhmurl(const CString &moduleName, const CString &bundleName, 189 const CString &pkgName, const CString &entryPath, 190 const CString &version); 191 static CString ConcatMergeFileNameToNormalized(JSThread *thread, const JSPandaFile *jsPandaFile, 192 CString &baseFileName, const CString &recordName, 193 CString requestName); 194 static CVector<CString> SplitNormalizedRecordName(const CString &recordName); 195 static CString ConcatImportFileNormalizedOhmurlWithRecordName(JSThread *thread, const JSPandaFile *jsPandaFile, 196 CString &baseFileName, const CString &recordName, 197 const CString &requestName); 198 static void ConcatOtherNormalizedOhmurl(EcmaVM *vm, const JSPandaFile *jsPandaFile, 199 [[maybe_unused]] CString &baseFileName, CString &requestPath); 200 static CString ConcatNormalizedOhmurlWithData(CVector<CString> &data, CString &pkgName, CString &entryPath); 201 static CString GetBundleNameWithRecordName(EcmaVM *vm, const CString &recordName); 202 static CString Utf8ConvertToString(JSTaggedValue str); 203 204 static CString ParseFileNameToVMAName(const CString &filename); 205 static CString ConcatOtherNormalizedOhmurlWithFilePath(EcmaVM *vm, size_t filePathPos, CString &moduleName, 206 const CString &requestPath); 207 static bool IsOhmUrl(const CString &str); 208 static bool CheckAndGetRecordName(JSThread *thread, const CString &ohmUrl, CString &recordName); 209 static bool ValidateAbcPath(const CString &baseFileName, ValidateFilePath checkMode); 210 /* 211 * Before: /data/storage/el1/bundle/moduleName/ets/modules.abc 212 * After: bundle/moduleName 213 */ ParseHapPath(const CString & baseFileName)214 inline static std::string ParseHapPath(const CString &baseFileName) 215 { 216 CString bundleSubInstallName(BUNDLE_SUB_INSTALL_PATH); 217 size_t startStrLen = bundleSubInstallName.length(); 218 if (baseFileName.length() > startStrLen && baseFileName.compare(0, startStrLen, bundleSubInstallName) == 0) { 219 CString hapPath = baseFileName.substr(startStrLen); 220 size_t pos = hapPath.find(MERGE_ABC_ETS_MODULES); 221 if (pos != CString::npos) { 222 return hapPath.substr(0, pos).c_str(); 223 } 224 } 225 return std::string(); 226 } 227 228 /* 229 * Before: xxx 230 * After: xxx || xxx/index 231 */ ConfirmLoadingIndexOrNot(const JSPandaFile * jsPandaFile,const CString & packageEntryPoint)232 inline static CString ConfirmLoadingIndexOrNot(const JSPandaFile *jsPandaFile, const CString &packageEntryPoint) 233 { 234 CString entryPoint = packageEntryPoint; 235 if (jsPandaFile->HasRecord(entryPoint)) { 236 return entryPoint; 237 } 238 // Possible import directory 239 entryPoint += PACKAGE_ENTRY_FILE; 240 entryPoint = PathHelper::NormalizePath(entryPoint); 241 if (jsPandaFile->HasRecord(entryPoint)) { 242 return entryPoint; 243 } 244 return CString(); 245 } 246 IsNativeModuleRequest(const CString & requestName)247 inline static bool IsNativeModuleRequest(const CString &requestName) 248 { 249 if (requestName[0] != PathHelper::NAME_SPACE_TAG) { 250 return false; 251 } 252 if (StringHelper::StringStartWith(requestName, ModulePathHelper::REQUIRE_NAPI_OHOS_PREFIX) || 253 StringHelper::StringStartWith(requestName, ModulePathHelper::REQUIRE_NAPI_APP_PREFIX) || 254 StringHelper::StringStartWith(requestName, ModulePathHelper::REQUIRE_NAITVE_MODULE_PREFIX)) { 255 return true; 256 } 257 return false; 258 } 259 260 /* 261 * Before: bundleName/moduleName/ets/xxx/xxx 262 * After: moduleName 263 */ GetModuleName(const CString recordName)264 inline static CString GetModuleName(const CString recordName) 265 { 266 size_t pos1 = recordName.find(PathHelper::SLASH_TAG); 267 if (pos1 != CString::npos) { 268 pos1++; 269 size_t pos2 = recordName.find(PathHelper::SLASH_TAG, pos1); 270 if (pos2 != CString::npos) { 271 CString moduleName = recordName.substr(pos1, pos2 - pos1); 272 PathHelper::DeleteNamespace(moduleName); 273 return moduleName; 274 } 275 } 276 return CString(); 277 } 278 279 /* 280 * Before: &moduleName/src/xxx 281 * After: moduleName 282 */ GetModuleNameWithNormalizedName(const CString recordName)283 inline static CString GetModuleNameWithNormalizedName(const CString recordName) 284 { 285 size_t pos1 = recordName.find(PathHelper::NORMALIZED_OHMURL_TAG); 286 if (pos1 != CString::npos) { 287 pos1++; 288 size_t pos2 = recordName.find(PathHelper::SLASH_TAG, pos1); 289 if (pos2 != CString::npos) { 290 CString moduleName = recordName.substr(pos1, pos2 - pos1); 291 return moduleName; 292 } 293 } 294 return CString(); 295 } 296 297 /* 298 * Before: bundleName/moduleName 299 * After: moduleName 300 */ GetModuleNameWithPath(const CString modulePath)301 inline static CString GetModuleNameWithPath(const CString modulePath) 302 { 303 size_t pos1 = modulePath.find(PathHelper::SLASH_TAG); 304 if (pos1 != CString::npos) { 305 pos1++; 306 return modulePath.substr(pos1, modulePath.size() - pos1 + 1); 307 } 308 return CString(); 309 } 310 /* 311 * Before: @xxx. 312 * After: @xxx: 313 */ ChangeTag(CString & path)314 inline static bool ChangeTag(CString &path) 315 { 316 if (path[0] == PathHelper::NAME_SPACE_TAG) { 317 size_t pos = path.find(PathHelper::POINT_TAG); 318 if (pos != CString::npos) { 319 path.replace(pos, 1, PathHelper::COLON_TAG); // 1: length 320 return true; 321 } 322 } 323 return false; 324 } 325 326 /* 327 * Before: moduleName 328 * After: data/storage/el1/bundle/moduleName/ets/modules.abc 329 */ ConcatPandaFilePath(const CString & moduleName)330 inline static CString ConcatPandaFilePath(const CString &moduleName) 331 { 332 if (moduleName.size() == 0) { 333 return CString(); 334 } 335 return BUNDLE_INSTALL_PATH + moduleName + MERGE_ABC_ETS_MODULES; 336 } 337 GetBundleNameFromNormalized(const EcmaVM * vm,const CString & moduleName)338 inline static CString GetBundleNameFromNormalized(const EcmaVM *vm, const CString &moduleName) 339 { 340 CVector<CString> res = SplitNormalizedOhmurl(moduleName); 341 if (res.size() != NORMALIZED_OHMURL_ARGS_NUM) { 342 LOG_FULL(ERROR) << "GetBundleNameFromNormalized Invalid normalized ohmurl"; 343 return ""; 344 } 345 CString bundleName = res[NORMALIZED_BUNDLE_NAME_INDEX]; 346 if (bundleName.size() == 0) { 347 return vm->GetBundleName(); 348 } 349 return bundleName; 350 } 351 GetNormalizedPathFromOhmUrl(const CString & moduleName)352 inline static CString GetNormalizedPathFromOhmUrl(const CString &moduleName) 353 { 354 CVector<CString> res = SplitNormalizedOhmurl(moduleName); 355 if (res.size() != NORMALIZED_OHMURL_ARGS_NUM) { 356 LOG_FULL(ERROR) << "GetNormalizedPathFromOhmUrl Invalid normalized ohmurl"; 357 return ""; 358 } 359 CString soName = res[NORMALIZED_IMPORT_PATH_INDEX]; 360 // Delete the prefix "lib" and suffix ".so". 361 soName = soName.substr(SO_PREFIX_LEN, soName.size() - SO_PREFIX_LEN - SO_SUFFIX_LEN); 362 return soName; 363 } 364 }; 365 } // namespace panda::ecmascript 366 #endif // ECMASCRIPT_MODULE_MODULE_PATH_HELPER_H