• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef ES2PANDA_UTIL_IMPORT_PATH_MANAGER_H
17 #define ES2PANDA_UTIL_IMPORT_PATH_MANAGER_H
18 
19 #if defined PANDA_TARGET_MOBILE
20 #define USE_UNIX_SYSCALL
21 #endif
22 
23 #include "util/arktsconfig.h"
24 #include "util/ustring.h"
25 #include "es2panda.h"
26 
27 namespace ark::es2panda::util {
28 
29 class ImportPathManager {
30 public:
31     struct ImportData {
32         Language lang;
33         std::string module;
34         bool hasDecl;
35     };
36 
37     struct ParseInfo {
38         StringView sourcePath;
39         bool isParsed;
40     };
41 
ImportPathManager(ark::ArenaAllocator * allocator,std::shared_ptr<ArkTsConfig> arktsConfig,std::string stdLib)42     ImportPathManager(ark::ArenaAllocator *allocator, std::shared_ptr<ArkTsConfig> arktsConfig, std::string stdLib)
43         : allocator_(allocator),
44           arktsConfig_(std::move(arktsConfig)),
45           stdLib_(std::move(stdLib)),
46           parseList_(allocator->Adapter())
47     {
48     }
49 
50     NO_COPY_SEMANTIC(ImportPathManager);
51     NO_MOVE_SEMANTIC(ImportPathManager);
52     ImportPathManager() = delete;
53     ~ImportPathManager() = default;
54 
55     StringView ResolvePath(const StringView &currentModulePath, const StringView &importPath) const;
56     void AddToParseList(const StringView &path, bool isDefaultImport);
57     const ArenaVector<ParseInfo> &ParseList();
58     ImportData GetImportData(const util::StringView &path, const ScriptExtension &extension) const;
59     void MarkAsParsed(const StringView &path);
60 
61 private:
62     bool IsRelativePath(const StringView &path) const;
63     StringView GetRealPath(const StringView &path) const;
64     StringView AppendExtensionOrIndexFileIfOmitted(const StringView &path) const;
65 #ifdef USE_UNIX_SYSCALL
66     void UnixWalkThroughDirectoryAndAddToParseList(const StringView &directoryPath, bool isDefaultImport);
67 #endif
68 
69     ArenaAllocator *allocator_ {nullptr};
70     std::shared_ptr<ArkTsConfig> arktsConfig_ {nullptr};
71     std::string stdLib_ {};
72     ArenaVector<ParseInfo> parseList_;
73     std::string_view pathDelimiter_ {ark::os::file::File::GetPathDelim()};
74 };
75 
76 }  // namespace ark::es2panda::util
77 
78 #endif  // ES2PANDA_UTIL_IMPORT_PATH_MANAGER_H
79