1 /*
2  * Copyright (c) 2021 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 FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_SOURCE_MAP_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_SOURCE_MAP_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "base/memory/referenced.h"
25 #include "base/utils/macros.h"
26 
27 namespace OHOS::Ace::Framework {
28 
29 struct SourceMapInfo {
30     int32_t beforeRow = 0;
31     int32_t beforeColumn = 0;
32     int32_t afterRow = 0;
33     int32_t afterColumn = 0;
34     int32_t sourcesVal = 0;
35     int32_t namesVal = 0;
36 };
37 
38 struct MappingInfo {
39     int32_t row = 0;
40     int32_t col = 0;
41     std::string sources;
42 };
43 
44 class ACE_FORCE_EXPORT RevSourceMap final : public Referenced {
45 public:
46     MappingInfo Find(int32_t row, int32_t col, bool isColPrecise = true);
47     std::string GetOriginalNames(const std::string& sourceCode, uint32_t& errorPos) const;
48     static void ExtractKeyInfo(const std::string& sourceMap, std::vector<std::string>& sourceKeyInfo);
49     void Init(const std::string& sourceMap);
50     static void MergeInit(const std::string& sourceMap, RefPtr<RevSourceMap>& RevSourceMap);
51     static void StageModeSourceMapSplit(
52         const std::string& sourceMap, std::unordered_map<std::string, RefPtr<RevSourceMap>>& sourceMaps);
53 
54 private:
55     SourceMapInfo nowPos_;
56     std::vector<std::string> files_;
57     std::vector<std::string> sources_;
58     std::vector<std::string> names_;
59     std::vector<std::string> mappings_;
60     std::vector<std::string> nameMap_;
61     std::vector<SourceMapInfo> afterPos_;
62 
63     static std::vector<std::string> HandleMappings(const std::string& mapping);
64     static uint32_t Base64CharToInt(char charCode);
65     static bool VlqRevCode(const std::string& vStr, std::vector<int32_t>& ans);
66 };
67 
68 }  // namespace OHOS::Ace::Framework
69 
70 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_SOURCE_MAP_H
71