1 /* 2 * Copyright (c) 2023 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_BASE_PATH_HELPER_H 16 #define ECMASCRIPT_BASE_PATH_HELPER_H 17 18 #include "ecmascript/compiler/aot_file/aot_file_manager.h" 19 #include "ecmascript/base/string_helper.h" 20 #include "ecmascript/ecma_macros.h" 21 #include "ecmascript/ecma_string.h" 22 #include "ecmascript/ecma_vm.h" 23 #include "ecmascript/global_env.h" 24 #include "ecmascript/js_tagged_value-inl.h" 25 #include "ecmascript/jspandafile/js_pandafile.h" 26 27 namespace panda::ecmascript::base { 28 class PathHelper { 29 public: 30 static constexpr char COLON_TAG = ':'; 31 static constexpr char CURRENT_DIREATORY_TAG[] = "./"; 32 static constexpr char DOUBLE_POINT_TAG[] = ".."; 33 static constexpr char DOUBLE_SLASH_TAG[] = "//"; 34 static constexpr char NAME_SPACE_TAG = '@'; 35 static constexpr char POINT_STRING_TAG[] = "."; 36 static constexpr char POINT_TAG = '.'; 37 static constexpr char SLASH_TAG = '/'; 38 39 static CString NormalizePath(const CString &fileName); 40 static JSHandle<EcmaString> ResolveDirPath(JSThread *thread, CString fileName); 41 42 /* 43 * Before: moduleName@nameSpace 44 * After: moduleName 45 */ DeleteNamespace(CString & moduleName)46 inline static void DeleteNamespace(CString &moduleName) 47 { 48 size_t pos = moduleName.find(NAME_SPACE_TAG); 49 if (pos == CString::npos) { 50 return; 51 } 52 moduleName.erase(pos, moduleName.size() - pos); 53 } 54 55 /* 56 * Before: bundleName/moduleName@namespace/moduleName/xxx/xxx 57 * After: moduleName/xxx/xxx 58 */ AdaptOldIsaRecord(CString & recordName)59 inline static void AdaptOldIsaRecord(CString &recordName) 60 { 61 size_t pos = recordName.find(SLASH_TAG); 62 if (pos != CString::npos) { 63 pos = recordName.find(SLASH_TAG, pos + 1); 64 if (pos != CString::npos) { 65 recordName = recordName.substr(pos + 1); 66 } 67 } 68 } 69 70 /* 71 * Before: @***:xxxx 72 * After: xxxx 73 */ GetStrippedModuleName(const CString & moduleRequestName)74 inline static CString GetStrippedModuleName(const CString &moduleRequestName) 75 { 76 size_t pos = moduleRequestName.find(COLON_TAG); 77 if (pos == CString::npos) { 78 LOG_FULL(FATAL) << "Unknown format " << moduleRequestName; 79 } 80 return moduleRequestName.substr(pos + 1); 81 } 82 83 /* 84 * Before: @xxx:**** 85 * After: xxx 86 */ GetInternalModulePrefix(const CString & moduleRequestName)87 inline static CString GetInternalModulePrefix(const CString &moduleRequestName) 88 { 89 size_t pos = moduleRequestName.find(COLON_TAG); 90 if (pos == CString::npos) { 91 LOG_FULL(FATAL) << "Unknown format " << moduleRequestName; 92 } 93 return moduleRequestName.substr(1, pos - 1); 94 } 95 }; 96 } // namespace panda::ecmascript::base 97 #endif // ECMASCRIPT_BASE_PATH_HELPER_H