• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H
17 #define OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H
18 
19 #include <cstring>
20 #include <fstream>
21 #include <limits.h>
22 #include <mutex>
23 #include <unordered_map>
24 #include <utility>
25 #include <thread>
26 #include <vector>
27 
28 #include "ecmascript/log_wrapper.h"
29 
30 namespace panda {
31 namespace ecmascript {
32 using Clock = std::chrono::high_resolution_clock;
33 
34 struct SourceMapInfo {
35     int32_t beforeRow = 0;
36     int32_t beforeColumn = 0;
37     int32_t afterRow = 0;
38     int32_t afterColumn = 0;
39     int32_t sourcesVal = 0;
40     int32_t namesVal = 0;
41 };
42 
43 struct MappingInfo {
44     int32_t row = 0;
45     int32_t col = 0;
46 };
47 
48 class SourceMapData final {
49 public:
50     SourceMapData() = default;
51     ~SourceMapData() = default;
52 
53     std::string url_;
54     SourceMapInfo nowPos_;
55     std::vector<std::string> sources_;
56     std::vector<std::string> mappings_;
57     std::vector<SourceMapInfo> afterPos_;
58 
GetSourceMapData()59     inline SourceMapData GetSourceMapData() const
60     {
61         return *this;
62     }
63 };
64 
65 class SourceMap final {
66 public:
67     SourceMap() = default;
68     ~SourceMap() = default;
69 
70 #if defined(PANDA_TARGET_OHOS)
71     void Init(const std::string& hapPath);
72 #endif
73     void Init(uint8_t *data, size_t dataSize);
74     bool TranslateUrlPositionBySourceMap(std::string& url, int& line, int& column, std::string& packageName);
75 
76 private:
77     void SplitSourceMap(const std::string& sourceMapData);
78     void ExtractSourceMapData(const std::string& sourceMapData, std::shared_ptr<SourceMapData>& curMapData);
79     void ExtractKeyInfo(const std::string& sourceMap, std::vector<std::string>& sourceKeyInfo);
80     std::vector<std::string> HandleMappings(const std::string& mapping);
81     bool VlqRevCode(const std::string& vStr, std::vector<int32_t>& ans);
82     MappingInfo Find(int32_t row, int32_t col, const SourceMapData& targetMap, bool& isReplaces);
83     void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column);
84     bool GetLineAndColumnNumbers(int& line, int& column, SourceMapData& targetMap, bool& isReplaces);
85     uint32_t Base64CharToInt(char charCode);
86     void GetPackageName(std::string& url, std::string& packageName);
87     friend class SourceMapFriend;
88 #if defined(PANDA_TARGET_OHOS)
89     bool ReadSourceMapData(const std::string& hapPath, std::string& content);
90 #endif
91 
92 private:
93     std::unordered_map<std::string, std::string> sources_;
94     std::unordered_map<std::string, std::string> mappings_;
95     std::unordered_map<std::string, std::shared_ptr<SourceMapData>> sourceMaps_;
96     std::unordered_map<std::string, std::string> entryPackageInfo_;
97     std::unordered_map<std::string, std::string> packageInfo_;
98     std::unordered_map<std::string, std::string> packageName_;
99 };
100 } // namespace panda
101 } // namespace ecmascript
102 
103 #endif // OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H
104