• 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 namespace OHOS {
29 namespace JsEnv {
30 namespace {
31 const std::string NOT_FOUNDMAP = "Cannot get SourceMap info, dump raw stack:\n";
32 }
33 using ErrorPos = std::pair<uint32_t, uint32_t>;
34 struct SourceMapInfo {
35     int32_t beforeRow = 0;
36     int32_t beforeColumn = 0;
37     int32_t afterRow = 0;
38     int32_t afterColumn = 0;
39 };
40 
41 struct MappingInfo {
42     int32_t row = 0;
43     int32_t col = 0;
44     std::string sources;
45 };
46 
47 class SourceMapData final {
48 public:
49     SourceMapData() = default;
50     ~SourceMapData() = default;
51 
52     SourceMapInfo nowPos_;
53     std::string packageName_;
54     bool isPackageInfo_ = false;
55     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 using ReadSourceMapCallback = std::function<bool(const std::string& hapPath,
66     const std::string& sourceMapPath, std::string& content)>;
67 using GetHapPathCallback = std::function<void(const std::string &bundleName, std::vector<std::string> &hapList)>;
68 class SourceMap final {
69 public:
70     SourceMap() = default;
71     ~SourceMap() = default;
72 
73     void Init(bool& hasFile, const std::string& hapPath);
74     std::string TranslateBySourceMap(const std::string& stackStr);
75     bool TranslateUrlPositionBySourceMap(std::string& url, int& line, int& column, std::string& packageName);
76     static ErrorPos GetErrorPos(const std::string& rawStack);
77     static void RegisterReadSourceMapCallback(ReadSourceMapCallback readFunc);
78     static bool ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content);
79     static void RegisterGetHapPathCallback(GetHapPathCallback getFunc);
80     static void GetHapPath(const std::string &bundleName, std::vector<std::string> &hapList);
81     bool GetLineAndColumnNumbers(int& line, int& column, SourceMapData& targetMap, std::string& url,
82                                  std::string& packageName);
83     static void ExtractStackInfo(const std::string& stackStr, std::vector<std::string>& res);
84     void SplitSourceMap(const std::string& sourceMapData);
85 
86 private:
87     void ExtractSourceMapData(const std::string& allmappings, std::shared_ptr<SourceMapData>& curMapData);
88     void ExtractKeyInfo(const std::string& sourceMap, std::vector<std::string>& sourceKeyInfo);
89     std::vector<std::string> HandleMappings(const std::string& mapping);
90     bool VlqRevCode(const std::string& vStr, std::vector<int32_t>& ans);
91     MappingInfo Find(int32_t row, int32_t col, const SourceMapData& targetMap, const std::string& key);
92     void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column);
93     std::string GetSourceInfo(const std::string& line, const std::string& column,
94                               const SourceMapData& targetMap, const std::string& key);
95     static void GetPackageName(const SourceMapData& targetMap, std::string& packageName);
96 
97 private:
98     static ReadSourceMapCallback readSourceMapFunc_;
99     static std::mutex sourceMapMutex_;
100     static GetHapPathCallback getHapPathFunc_;
101     std::unordered_map<std::string, std::shared_ptr<SourceMapData>> sourceMaps_;
102 };
103 } // namespace JsEnv
104 } // namespace OHOS
105 
106 #endif // OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H
107