14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MODULE_MODULE_PATH_HELPER_H
164514f5e3Sopenharmony_ci#define ECMASCRIPT_MODULE_MODULE_PATH_HELPER_H
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/base/path_helper.h"
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/aot_file_manager.h"
214514f5e3Sopenharmony_ci#include "ecmascript/base/string_helper.h"
224514f5e3Sopenharmony_ci#include "ecmascript/ecma_macros.h"
234514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h"
244514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
254514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
264514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h"
274514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/js_pandafile.h"
284514f5e3Sopenharmony_ci/*
294514f5e3Sopenharmony_ci * Intra-application cross hap:
304514f5e3Sopenharmony_ci * baseFileName = 'data/storage/el1/bundle/moduleName/ets/modules.abc';
314514f5e3Sopenharmony_ci * cross-application:
324514f5e3Sopenharmony_ci * baseFileName = 'data/storage/el1/bundle/bundleName/moduleName/moduleName/ets/modules.abc';
334514f5e3Sopenharmony_ci * recordName = bundleName/moduleName/xxx(entry)/xxx(ets)/xxx(pages)/xxx  specific abc file
344514f5e3Sopenharmony_ci *
354514f5e3Sopenharmony_ci * ohmUrl: It's an index information that can uniquely identify module files.
364514f5e3Sopenharmony_ci * Current ohmUrl has the following five different prefixs:
374514f5e3Sopenharmony_ci * 1. @bundle:... Identify OpenHarmony modules.
384514f5e3Sopenharmony_ci *    {project_path}\entry\src\main\ets\pages\Index --> @bundle:bundleName/moduleName/ets/pages/Index
394514f5e3Sopenharmony_ci *    @namespace: needs to add when import local har or ohosTest import entry file.
404514f5e3Sopenharmony_ci *    {project_path}\namespace\src\main\ets\pages\Index --> @bundle:bundleName/moduleName@namespace/ets/pages/Index
414514f5e3Sopenharmony_ci *
424514f5e3Sopenharmony_ci * 2. @package:... Identify open source third party modules.
434514f5e3Sopenharmony_ci *    {project_path}\node_modules.ohpm\pkgName\oh_modules\pkgName\xxx\xxx
444514f5e3Sopenharmony_ci *    --> @package:pkg_modules/.ohpm/pkgName/pkg_modules/pkgName/xxx/xxx
454514f5e3Sopenharmony_ci *
464514f5e3Sopenharmony_ci * 3. @app:... Identify c++ modules in application.
474514f5e3Sopenharmony_ci *    libxxx.so --> @app:bundleName/moduleName/xxx
484514f5e3Sopenharmony_ci *
494514f5e3Sopenharmony_ci * 4. @native:... Identify system builtin modules.
504514f5e3Sopenharmony_ci *    system.app --> @native:system.app
514514f5e3Sopenharmony_ci *
524514f5e3Sopenharmony_ci * 5. @ohos:... Identify ohos builtin modules.
534514f5e3Sopenharmony_ci *    @ohos:hilog --> @ohos:hilog
544514f5e3Sopenharmony_ci */
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_cinamespace panda::ecmascript {
574514f5e3Sopenharmony_ciusing PathHelper = base::PathHelper;
584514f5e3Sopenharmony_ciusing StringHelper = base::StringHelper;
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_cienum ValidateFilePath { ABC, ETS_MODULES };
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ciclass ModulePathHelper {
634514f5e3Sopenharmony_cipublic:
644514f5e3Sopenharmony_ci    static constexpr char EXT_NAME_ABC[] = ".abc";
654514f5e3Sopenharmony_ci    static constexpr char EXT_NAME_ETS[] = ".ets";
664514f5e3Sopenharmony_ci    static constexpr char EXT_NAME_TS[] = ".ts";
674514f5e3Sopenharmony_ci    static constexpr char EXT_NAME_JS[] = ".js";
684514f5e3Sopenharmony_ci    static constexpr char EXT_NAME_JSON[] = ".json";
694514f5e3Sopenharmony_ci    static constexpr char EXT_NAME_Z_SO[] = ".z.so";
704514f5e3Sopenharmony_ci    static constexpr char EXT_NAME_D_TS[] = ".d.ts";
714514f5e3Sopenharmony_ci    static constexpr char EXT_NAME_MJS[] = ".mjs";
724514f5e3Sopenharmony_ci    static constexpr char EXT_NAME_HQF[] = ".hqf";
734514f5e3Sopenharmony_ci    static constexpr char PREFIX_NORMALIZED[] = "@normalized:";
744514f5e3Sopenharmony_ci    static constexpr char PREFIX_NORMALIZED_SO[] = "@normalized:Y";
754514f5e3Sopenharmony_ci    static constexpr char PREFIX_NORMALIZED_NOT_SO[] = "@normalized:N";
764514f5e3Sopenharmony_ci    static constexpr char PREFIX_BUNDLE[] = "@bundle:";
774514f5e3Sopenharmony_ci    static constexpr char PREFIX_MODULE[] = "@module:";
784514f5e3Sopenharmony_ci    static constexpr char PREFIX_PACKAGE[] = "@package:";
794514f5e3Sopenharmony_ci    static constexpr char PREFIX_ETS[] = "ets/";
804514f5e3Sopenharmony_ci    static constexpr char PREFIX_LIB[] = "lib";
814514f5e3Sopenharmony_ci    static constexpr char REQUIRE_NAITVE_MODULE_PREFIX[] = "@native:";
824514f5e3Sopenharmony_ci    static constexpr char REQUIRE_NAPI_OHOS_PREFIX[] = "@ohos:";
834514f5e3Sopenharmony_ci    static constexpr char REQUIRE_NAPI_APP_PREFIX[] = "@app:";
844514f5e3Sopenharmony_ci    static constexpr char RAW_ARKUIX_PREFIX[] = "@arkui-x.";
854514f5e3Sopenharmony_ci    static constexpr char NPM_PATH_SEGMENT[] = "node_modules";
864514f5e3Sopenharmony_ci    static constexpr char PACKAGE_PATH_SEGMENT[] = "pkg_modules";
874514f5e3Sopenharmony_ci    static constexpr char PACKAGE_ENTRY_FILE[] = "/index";
884514f5e3Sopenharmony_ci    static constexpr char BUNDLE_INSTALL_PATH[] = "/data/storage/el1/bundle/";
894514f5e3Sopenharmony_ci    static constexpr char MERGE_ABC_ETS_MODULES[] = "/ets/modules.abc";
904514f5e3Sopenharmony_ci    static constexpr char ABC[] = ".abc";
914514f5e3Sopenharmony_ci    static constexpr char MODULE_DEFAULE_ETS[] = "/ets/";
924514f5e3Sopenharmony_ci    static constexpr char BUNDLE_SUB_INSTALL_PATH[] = "/data/storage/el1/";
934514f5e3Sopenharmony_ci    static constexpr char PREVIEW_OF_ACROSS_HAP_FLAG[] = "[preview]";
944514f5e3Sopenharmony_ci    static constexpr char PREVIER_TEST_DIR[] = ".test";
954514f5e3Sopenharmony_ci    static constexpr char PHYCICAL_FILE_PATH[] = "/src/main";
964514f5e3Sopenharmony_ci    static constexpr char VMA_NAME_ARKTS_CODE[] = "ArkTS Code";
974514f5e3Sopenharmony_ci    static constexpr char ENTRY_MAIN_FUNCTION[] = "_GLOBAL::func_main_0";
984514f5e3Sopenharmony_ci    static constexpr char TRUE_FLAG[] = "true";
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ci    static constexpr size_t MAX_PACKAGE_LEVEL = 1;
1014514f5e3Sopenharmony_ci    static constexpr size_t SEGMENTS_LIMIT_TWO = 2;
1024514f5e3Sopenharmony_ci    static constexpr size_t EXT_NAME_ABC_LEN = 4;
1034514f5e3Sopenharmony_ci    static constexpr size_t EXT_NAME_ETS_LEN = 4;
1044514f5e3Sopenharmony_ci    static constexpr size_t EXT_NAME_TS_LEN = 3;
1054514f5e3Sopenharmony_ci    static constexpr size_t EXT_NAME_JS_LEN = 3;
1064514f5e3Sopenharmony_ci    static constexpr size_t EXT_NAME_JSON_LEN = 5;
1074514f5e3Sopenharmony_ci    static constexpr size_t PREFIX_BUNDLE_LEN = 8;
1084514f5e3Sopenharmony_ci    static constexpr size_t PREFIX_MODULE_LEN = 8;
1094514f5e3Sopenharmony_ci    static constexpr size_t PREFIX_PACKAGE_LEN = 9;
1104514f5e3Sopenharmony_ci    static constexpr size_t PACKAGE_PATH_SEGMENT_LEN = 11;
1114514f5e3Sopenharmony_ci    static constexpr size_t NATIVE_PREFIX_SIZE = 8;
1124514f5e3Sopenharmony_ci    static constexpr size_t OHOS_PREFIX_SIZE = 6;
1134514f5e3Sopenharmony_ci    static constexpr size_t APP_PREFIX_SIZE = 5;
1144514f5e3Sopenharmony_ci    static constexpr size_t BUNDLE_INSTALL_PATH_LEN = 25;
1154514f5e3Sopenharmony_ci    static constexpr size_t PHYCICAL_FILE_PATH_LEN = 10;
1164514f5e3Sopenharmony_ci    static constexpr size_t NORMALIZED_OHMURL_ARGS_NUM = 5;
1174514f5e3Sopenharmony_ci    static constexpr size_t NORMALIZED_MODULE_NAME_INDEX = 1;
1184514f5e3Sopenharmony_ci    static constexpr size_t NORMALIZED_BUNDLE_NAME_INDEX = 2;
1194514f5e3Sopenharmony_ci    static constexpr size_t NORMALIZED_IMPORT_PATH_INDEX = 3;
1204514f5e3Sopenharmony_ci    static constexpr size_t NORMALIZED_VERSION_INDEX = 4;
1214514f5e3Sopenharmony_ci    static constexpr size_t CURRENT_DIREATORY_TAG_LEN = 2;
1224514f5e3Sopenharmony_ci    static constexpr size_t SO_PREFIX_LEN = 3;
1234514f5e3Sopenharmony_ci    static constexpr size_t SO_SUFFIX_LEN = 3;
1244514f5e3Sopenharmony_ci
1254514f5e3Sopenharmony_ci    static constexpr size_t PKGINFO_PACKAGE_NAME_INDEX = 1;
1264514f5e3Sopenharmony_ci    static constexpr size_t PKGINFO_BUDNLE_NAME_INDEX = 3;
1274514f5e3Sopenharmony_ci    static constexpr size_t PKGINFO_MODULE_NAME_INDEX = 5;
1284514f5e3Sopenharmony_ci    static constexpr size_t PKGINFO_VERSION_INDEX = 7;
1294514f5e3Sopenharmony_ci    static constexpr size_t PKGINFO_ENTRY_PATH_INDEX = 9;
1304514f5e3Sopenharmony_ci    static constexpr size_t PKGINFO_IS_SO_INDEX = 11;
1314514f5e3Sopenharmony_ci
1324514f5e3Sopenharmony_ci    static CString PUBLIC_API ConcatFileNameWithMerge(JSThread *thread, const JSPandaFile *jsPandaFile,
1334514f5e3Sopenharmony_ci                                                      CString &baseFileName, CString recordName, CString requestName);
1344514f5e3Sopenharmony_ci    static void ParseAbcPathAndOhmUrl(EcmaVM *vm, const CString &inputFileName, CString &outBaseFileName,
1354514f5e3Sopenharmony_ci                                      CString &outEntryPoint);
1364514f5e3Sopenharmony_ci    static CString ConcatUnifiedOhmUrl(const CString &bundleName, const CString &pkgname, const CString &entryPath,
1374514f5e3Sopenharmony_ci                                       const CString &path, const CString &version);
1384514f5e3Sopenharmony_ci    static CString ConcatUnifiedOhmUrl(const CString &bundleName, const CString &normalizedpath,
1394514f5e3Sopenharmony_ci        const CString &version);
1404514f5e3Sopenharmony_ci    static CString ConcatPreviewTestUnifiedOhmUrl(const CString &bundleName, const CString &pkgname,
1414514f5e3Sopenharmony_ci        const CString &path, const CString &version);
1424514f5e3Sopenharmony_ci    static CString ConcatHspFileNameCrossBundle(const CString &bundleName, const CString &moduleName);
1434514f5e3Sopenharmony_ci    static CString ConcatHspFileName(const CString &moduleName);
1444514f5e3Sopenharmony_ci    static CString TransformToNormalizedOhmUrl(EcmaVM *vm, const CString &inputFileName, const CString &baseFileName,
1454514f5e3Sopenharmony_ci        const CString &oldEntryPoint);
1464514f5e3Sopenharmony_ci    static CString ParseUrl(EcmaVM *vm, const CString &recordName);
1474514f5e3Sopenharmony_ci    static CString ParsePrefixBundle(JSThread *thread, const JSPandaFile *jsPandaFile,
1484514f5e3Sopenharmony_ci        [[maybe_unused]] CString &baseFileName, CString moduleRequestName, [[maybe_unused]] CString recordName);
1494514f5e3Sopenharmony_ci    static CString ParseNormalizedOhmUrl(JSThread *thread, CString &baseFileName, const CString &recordName,
1504514f5e3Sopenharmony_ci                                         CString requestName);
1514514f5e3Sopenharmony_ci    static CString MakeNewRecord(JSThread *thread, const JSPandaFile *jsPandaFile, CString &baseFileName,
1524514f5e3Sopenharmony_ci                                 const CString &recordName, const CString &requestName);
1534514f5e3Sopenharmony_ci    static CString FindOhpmEntryPoint(const JSPandaFile *jsPandaFile, const CString &ohpmPath,
1544514f5e3Sopenharmony_ci                                      const CString &requestName);
1554514f5e3Sopenharmony_ci    static CString FindPackageInTopLevelWithNamespace(const JSPandaFile *jsPandaFile, const CString &requestName,
1564514f5e3Sopenharmony_ci                                                      const CString &recordName);
1574514f5e3Sopenharmony_ci    static CString ParseOhpmPackage(const JSPandaFile *jsPandaFile, const CString &recordName,
1584514f5e3Sopenharmony_ci                                    const CString &requestName);
1594514f5e3Sopenharmony_ci    static CString ParseThirdPartyPackage(const JSPandaFile *jsPandaFile, const CString &recordName,
1604514f5e3Sopenharmony_ci                                          const CString &requestName, const CString &packagePath);
1614514f5e3Sopenharmony_ci    static CString ParseThirdPartyPackage(const JSPandaFile *jsPandaFile, const CString &recordName,
1624514f5e3Sopenharmony_ci                                          const CString &requestName);
1634514f5e3Sopenharmony_ci    static void ResolveCurrentPath(CString &dirPath, CString &fileName, const JSPandaFile *jsPandaFile);
1644514f5e3Sopenharmony_ci    static CString FindNpmEntryPoint(const JSPandaFile *jsPandaFile, const CString &packageEntryPoint);
1654514f5e3Sopenharmony_ci    static CString FindPackageInTopLevel(const JSPandaFile *jsPandaFile, const CString &requestName,
1664514f5e3Sopenharmony_ci                                         const CString &packagePath);
1674514f5e3Sopenharmony_ci    static bool IsImportFile(const CString &moduleRequestName);
1684514f5e3Sopenharmony_ci    static CString RemoveSuffix(const CString &requestName);
1694514f5e3Sopenharmony_ci    static bool NeedTranstale(const CString &requestName);
1704514f5e3Sopenharmony_ci    static bool NeedTranslateToNormalized(const CString &requestName);
1714514f5e3Sopenharmony_ci    static void TranstaleExpressionInput(const JSPandaFile *jsPandaFile, CString &requestPath);
1724514f5e3Sopenharmony_ci    static CString GetModuleNameWithBaseFile(const CString &baseFileName);
1734514f5e3Sopenharmony_ci    static CString TranslateExpressionInputWithEts(JSThread *thread, const JSPandaFile *jsPandaFile,
1744514f5e3Sopenharmony_ci                                                   CString &baseFileName, const CString &requestName);
1754514f5e3Sopenharmony_ci    static void ParseCrossModuleFile(const JSPandaFile *jsPandaFile, CString &requestPath);
1764514f5e3Sopenharmony_ci    static CString ReformatPath(CString requestName);
1774514f5e3Sopenharmony_ci    static CString TranslateExpressionToNormalized(JSThread *thread, const JSPandaFile *jsPandaFile,
1784514f5e3Sopenharmony_ci                                                   [[maybe_unused]] CString &baseFileName, const CString &recordName,
1794514f5e3Sopenharmony_ci                                                   CString &requestPath);
1804514f5e3Sopenharmony_ci    static CVector<CString> GetPkgContextInfoListElements(EcmaVM *vm, CString &moduleName,
1814514f5e3Sopenharmony_ci                                                          CString &packageName);
1824514f5e3Sopenharmony_ci    static CString TranslateNapiFileRequestPath(JSThread *thread, const CString &modulePath,
1834514f5e3Sopenharmony_ci                                                const CString &requestName);
1844514f5e3Sopenharmony_ci    static CVector<CString> SplitNormalizedOhmurl(const CString &ohmurl);
1854514f5e3Sopenharmony_ci    static CString ConcatImportFileNormalizedOhmurl(const CString &recordPath, const CString &requestName,
1864514f5e3Sopenharmony_ci                                                    const CString &version = "");
1874514f5e3Sopenharmony_ci    static CString ConcatNativeSoNormalizedOhmurl(const CString &moduleName, const CString &bundleName,
1884514f5e3Sopenharmony_ci                                                  const CString &pkgName, const CString &version);
1894514f5e3Sopenharmony_ci    static CString ConcatNotSoNormalizedOhmurl(const CString &moduleName, const CString &bundleName,
1904514f5e3Sopenharmony_ci                                               const CString &pkgName, const CString &entryPath,
1914514f5e3Sopenharmony_ci                                               const CString &version);
1924514f5e3Sopenharmony_ci    static CString ConcatMergeFileNameToNormalized(JSThread *thread, const JSPandaFile *jsPandaFile,
1934514f5e3Sopenharmony_ci                                                   CString &baseFileName, const CString &recordName,
1944514f5e3Sopenharmony_ci                                                   CString requestName);
1954514f5e3Sopenharmony_ci    static CVector<CString> SplitNormalizedRecordName(const CString &recordName);
1964514f5e3Sopenharmony_ci    static CString ConcatImportFileNormalizedOhmurlWithRecordName(JSThread *thread, const JSPandaFile *jsPandaFile,
1974514f5e3Sopenharmony_ci                                                                  CString &baseFileName, const CString &recordName,
1984514f5e3Sopenharmony_ci                                                                  const CString &requestName);
1994514f5e3Sopenharmony_ci    static void ConcatOtherNormalizedOhmurl(EcmaVM *vm, const JSPandaFile *jsPandaFile,
2004514f5e3Sopenharmony_ci                                            [[maybe_unused]] CString &baseFileName, CString &requestPath);
2014514f5e3Sopenharmony_ci    static CString ConcatNormalizedOhmurlWithData(CVector<CString> &data, CString &pkgName, CString &entryPath);
2024514f5e3Sopenharmony_ci    static CString GetBundleNameWithRecordName(EcmaVM *vm, const CString &recordName);
2034514f5e3Sopenharmony_ci    static CString Utf8ConvertToString(JSTaggedValue str);
2044514f5e3Sopenharmony_ci
2054514f5e3Sopenharmony_ci    static CString ParseFileNameToVMAName(const CString &filename);
2064514f5e3Sopenharmony_ci    static CString ConcatOtherNormalizedOhmurlWithFilePath(EcmaVM *vm, size_t filePathPos, CString &moduleName,
2074514f5e3Sopenharmony_ci                                                           const CString &requestPath);
2084514f5e3Sopenharmony_ci    static bool IsOhmUrl(const CString &str);
2094514f5e3Sopenharmony_ci    static bool CheckAndGetRecordName(JSThread *thread, const CString &ohmUrl, CString &recordName);
2104514f5e3Sopenharmony_ci    static bool ValidateAbcPath(const CString &baseFileName, ValidateFilePath checkMode);
2114514f5e3Sopenharmony_ci    /*
2124514f5e3Sopenharmony_ci     * Before: /data/storage/el1/bundle/moduleName/ets/modules.abc
2134514f5e3Sopenharmony_ci     * After:  bundle/moduleName
2144514f5e3Sopenharmony_ci     */
2154514f5e3Sopenharmony_ci    inline static std::string ParseHapPath(const CString &baseFileName)
2164514f5e3Sopenharmony_ci    {
2174514f5e3Sopenharmony_ci        CString bundleSubInstallName(BUNDLE_SUB_INSTALL_PATH);
2184514f5e3Sopenharmony_ci        size_t startStrLen = bundleSubInstallName.length();
2194514f5e3Sopenharmony_ci        if (baseFileName.length() > startStrLen && baseFileName.compare(0, startStrLen, bundleSubInstallName) == 0) {
2204514f5e3Sopenharmony_ci            CString hapPath = baseFileName.substr(startStrLen);
2214514f5e3Sopenharmony_ci            size_t pos = hapPath.find(MERGE_ABC_ETS_MODULES);
2224514f5e3Sopenharmony_ci            if (pos != CString::npos) {
2234514f5e3Sopenharmony_ci                return hapPath.substr(0, pos).c_str();
2244514f5e3Sopenharmony_ci            }
2254514f5e3Sopenharmony_ci        }
2264514f5e3Sopenharmony_ci        return std::string();
2274514f5e3Sopenharmony_ci    }
2284514f5e3Sopenharmony_ci
2294514f5e3Sopenharmony_ci    /*
2304514f5e3Sopenharmony_ci     * Before: xxx
2314514f5e3Sopenharmony_ci     * After:  xxx || xxx/index
2324514f5e3Sopenharmony_ci     */
2334514f5e3Sopenharmony_ci    inline static CString ConfirmLoadingIndexOrNot(const JSPandaFile *jsPandaFile, const CString &packageEntryPoint)
2344514f5e3Sopenharmony_ci    {
2354514f5e3Sopenharmony_ci        CString entryPoint = packageEntryPoint;
2364514f5e3Sopenharmony_ci        if (jsPandaFile->HasRecord(entryPoint)) {
2374514f5e3Sopenharmony_ci            return entryPoint;
2384514f5e3Sopenharmony_ci        }
2394514f5e3Sopenharmony_ci        // Possible import directory
2404514f5e3Sopenharmony_ci        entryPoint += PACKAGE_ENTRY_FILE;
2414514f5e3Sopenharmony_ci        entryPoint = PathHelper::NormalizePath(entryPoint);
2424514f5e3Sopenharmony_ci        if (jsPandaFile->HasRecord(entryPoint)) {
2434514f5e3Sopenharmony_ci            return entryPoint;
2444514f5e3Sopenharmony_ci        }
2454514f5e3Sopenharmony_ci        return CString();
2464514f5e3Sopenharmony_ci    }
2474514f5e3Sopenharmony_ci
2484514f5e3Sopenharmony_ci    inline static bool IsNativeModuleRequest(const CString &requestName)
2494514f5e3Sopenharmony_ci    {
2504514f5e3Sopenharmony_ci        if (requestName[0] != PathHelper::NAME_SPACE_TAG) {
2514514f5e3Sopenharmony_ci            return false;
2524514f5e3Sopenharmony_ci        }
2534514f5e3Sopenharmony_ci        if (StringHelper::StringStartWith(requestName, ModulePathHelper::REQUIRE_NAPI_OHOS_PREFIX) ||
2544514f5e3Sopenharmony_ci            StringHelper::StringStartWith(requestName, ModulePathHelper::REQUIRE_NAPI_APP_PREFIX) ||
2554514f5e3Sopenharmony_ci            StringHelper::StringStartWith(requestName, ModulePathHelper::REQUIRE_NAITVE_MODULE_PREFIX)) {
2564514f5e3Sopenharmony_ci            return true;
2574514f5e3Sopenharmony_ci        }
2584514f5e3Sopenharmony_ci        return false;
2594514f5e3Sopenharmony_ci    }
2604514f5e3Sopenharmony_ci
2614514f5e3Sopenharmony_ci    /*
2624514f5e3Sopenharmony_ci     * Before: bundleName/moduleName/ets/xxx/xxx
2634514f5e3Sopenharmony_ci     * After:  moduleName
2644514f5e3Sopenharmony_ci     */
2654514f5e3Sopenharmony_ci    inline static CString GetModuleName(const CString recordName)
2664514f5e3Sopenharmony_ci    {
2674514f5e3Sopenharmony_ci        size_t pos1 = recordName.find(PathHelper::SLASH_TAG);
2684514f5e3Sopenharmony_ci        if (pos1 != CString::npos) {
2694514f5e3Sopenharmony_ci            pos1++;
2704514f5e3Sopenharmony_ci            size_t pos2 = recordName.find(PathHelper::SLASH_TAG, pos1);
2714514f5e3Sopenharmony_ci            if (pos2 != CString::npos) {
2724514f5e3Sopenharmony_ci                CString moduleName = recordName.substr(pos1, pos2 - pos1);
2734514f5e3Sopenharmony_ci                PathHelper::DeleteNamespace(moduleName);
2744514f5e3Sopenharmony_ci                return moduleName;
2754514f5e3Sopenharmony_ci            }
2764514f5e3Sopenharmony_ci        }
2774514f5e3Sopenharmony_ci        return CString();
2784514f5e3Sopenharmony_ci    }
2794514f5e3Sopenharmony_ci
2804514f5e3Sopenharmony_ci    /*
2814514f5e3Sopenharmony_ci     * Before: &moduleName/src/xxx
2824514f5e3Sopenharmony_ci     * After:  moduleName
2834514f5e3Sopenharmony_ci     */
2844514f5e3Sopenharmony_ci    inline static CString GetModuleNameWithNormalizedName(const CString recordName)
2854514f5e3Sopenharmony_ci    {
2864514f5e3Sopenharmony_ci        size_t pos1 = recordName.find(PathHelper::NORMALIZED_OHMURL_TAG);
2874514f5e3Sopenharmony_ci        if (pos1 != CString::npos) {
2884514f5e3Sopenharmony_ci            pos1++;
2894514f5e3Sopenharmony_ci            size_t pos2 = recordName.find(PathHelper::SLASH_TAG, pos1);
2904514f5e3Sopenharmony_ci            if (pos2 != CString::npos) {
2914514f5e3Sopenharmony_ci                CString moduleName = recordName.substr(pos1, pos2 - pos1);
2924514f5e3Sopenharmony_ci                return moduleName;
2934514f5e3Sopenharmony_ci            }
2944514f5e3Sopenharmony_ci        }
2954514f5e3Sopenharmony_ci        return CString();
2964514f5e3Sopenharmony_ci    }
2974514f5e3Sopenharmony_ci
2984514f5e3Sopenharmony_ci    /*
2994514f5e3Sopenharmony_ci     * Before: bundleName/moduleName
3004514f5e3Sopenharmony_ci     * After:  moduleName
3014514f5e3Sopenharmony_ci     */
3024514f5e3Sopenharmony_ci    inline static CString GetModuleNameWithPath(const CString modulePath)
3034514f5e3Sopenharmony_ci    {
3044514f5e3Sopenharmony_ci        size_t pos1 = modulePath.find(PathHelper::SLASH_TAG);
3054514f5e3Sopenharmony_ci        if (pos1 != CString::npos) {
3064514f5e3Sopenharmony_ci            pos1++;
3074514f5e3Sopenharmony_ci            return modulePath.substr(pos1, modulePath.size() - pos1 + 1);
3084514f5e3Sopenharmony_ci        }
3094514f5e3Sopenharmony_ci        return CString();
3104514f5e3Sopenharmony_ci    }
3114514f5e3Sopenharmony_ci    /*
3124514f5e3Sopenharmony_ci     * Before: @xxx.
3134514f5e3Sopenharmony_ci     * After:  @xxx:
3144514f5e3Sopenharmony_ci     */
3154514f5e3Sopenharmony_ci    inline static bool ChangeTag(CString &path)
3164514f5e3Sopenharmony_ci    {
3174514f5e3Sopenharmony_ci        if (path[0] == PathHelper::NAME_SPACE_TAG) {
3184514f5e3Sopenharmony_ci            size_t pos = path.find(PathHelper::POINT_TAG);
3194514f5e3Sopenharmony_ci            if (pos != CString::npos) {
3204514f5e3Sopenharmony_ci                path.replace(pos, 1, PathHelper::COLON_TAG); // 1: length
3214514f5e3Sopenharmony_ci                return true;
3224514f5e3Sopenharmony_ci            }
3234514f5e3Sopenharmony_ci        }
3244514f5e3Sopenharmony_ci        return false;
3254514f5e3Sopenharmony_ci    }
3264514f5e3Sopenharmony_ci
3274514f5e3Sopenharmony_ci    /*
3284514f5e3Sopenharmony_ci     * Before: moduleName
3294514f5e3Sopenharmony_ci     * After:  data/storage/el1/bundle/moduleName/ets/modules.abc
3304514f5e3Sopenharmony_ci     */
3314514f5e3Sopenharmony_ci    inline static CString ConcatPandaFilePath(const CString &moduleName)
3324514f5e3Sopenharmony_ci    {
3334514f5e3Sopenharmony_ci        if (moduleName.size() == 0) {
3344514f5e3Sopenharmony_ci            return CString();
3354514f5e3Sopenharmony_ci        }
3364514f5e3Sopenharmony_ci        return BUNDLE_INSTALL_PATH + moduleName + MERGE_ABC_ETS_MODULES;
3374514f5e3Sopenharmony_ci    }
3384514f5e3Sopenharmony_ci
3394514f5e3Sopenharmony_ci    inline static CString GetBundleNameFromNormalized(const EcmaVM *vm, const CString &moduleName)
3404514f5e3Sopenharmony_ci    {
3414514f5e3Sopenharmony_ci        CVector<CString> res = SplitNormalizedOhmurl(moduleName);
3424514f5e3Sopenharmony_ci        if (res.size() != NORMALIZED_OHMURL_ARGS_NUM) {
3434514f5e3Sopenharmony_ci            LOG_FULL(ERROR) << "GetBundleNameFromNormalized Invalid normalized ohmurl";
3444514f5e3Sopenharmony_ci            return "";
3454514f5e3Sopenharmony_ci        }
3464514f5e3Sopenharmony_ci        CString bundleName = res[NORMALIZED_BUNDLE_NAME_INDEX];
3474514f5e3Sopenharmony_ci        if (bundleName.size() == 0) {
3484514f5e3Sopenharmony_ci            return vm->GetBundleName();
3494514f5e3Sopenharmony_ci        }
3504514f5e3Sopenharmony_ci        return bundleName;
3514514f5e3Sopenharmony_ci    }
3524514f5e3Sopenharmony_ci
3534514f5e3Sopenharmony_ci    inline static CString GetNormalizedPathFromOhmUrl(const CString &moduleName)
3544514f5e3Sopenharmony_ci    {
3554514f5e3Sopenharmony_ci        CVector<CString> res = SplitNormalizedOhmurl(moduleName);
3564514f5e3Sopenharmony_ci        if (res.size() != NORMALIZED_OHMURL_ARGS_NUM) {
3574514f5e3Sopenharmony_ci            LOG_FULL(ERROR) << "GetNormalizedPathFromOhmUrl Invalid normalized ohmurl";
3584514f5e3Sopenharmony_ci            return "";
3594514f5e3Sopenharmony_ci        }
3604514f5e3Sopenharmony_ci        CString soName = res[NORMALIZED_IMPORT_PATH_INDEX];
3614514f5e3Sopenharmony_ci        // Delete the prefix "lib" and suffix ".so".
3624514f5e3Sopenharmony_ci        soName = soName.substr(SO_PREFIX_LEN, soName.size() - SO_PREFIX_LEN - SO_SUFFIX_LEN);
3634514f5e3Sopenharmony_ci        return soName;
3644514f5e3Sopenharmony_ci    }
3654514f5e3Sopenharmony_ci};
3664514f5e3Sopenharmony_ci} // namespace panda::ecmascript
3674514f5e3Sopenharmony_ci#endif // ECMASCRIPT_MODULE_MODULE_PATH_HELPER_H