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 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 std::string sources; 47 }; 48 49 class SourceMapData final { 50 public: 51 SourceMapData() = default; 52 ~SourceMapData() = default; 53 54 SourceMapInfo nowPos_; 55 std::vector<std::string> files_; 56 std::vector<std::string> sources_; 57 std::vector<std::string> names_; 58 std::vector<std::string> mappings_; 59 std::vector<SourceMapInfo> afterPos_; 60 GetSourceMapData()61 inline SourceMapData GetSourceMapData() const 62 { 63 return *this; 64 } 65 }; 66 67 using ReadSourceMapCallback = std::function<bool(const std::string& hapPath, 68 const std::string& sourceMapPath, std::string& content)>; 69 using GetHapPathCallback = std::function<void(const std::string &bundleName, std::vector<std::string> &hapList)>; 70 class SourceMap final { 71 public: 72 SourceMap() = default; 73 ~SourceMap() = default; 74 75 void Init(bool isModular, const std::string& hapPath); 76 std::string TranslateBySourceMap(const std::string& stackStr); 77 bool TranslateUrlPositionBySourceMap(std::string& url, int& line, int& column); 78 static ErrorPos GetErrorPos(const std::string& rawStack); 79 static void RegisterReadSourceMapCallback(ReadSourceMapCallback readFunc); 80 static bool ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content); 81 static void RegisterGetHapPathCallback(GetHapPathCallback getFunc); 82 static void GetHapPath(const std::string &bundleName, std::vector<std::string> &hapList); 83 bool GetLineAndColumnNumbers(int& line, int& column, SourceMapData& targetMap, std::string& url); 84 static void ExtractStackInfo(const std::string& stackStr, std::vector<std::string>& res); 85 void SplitSourceMap(const std::string& sourceMapData); 86 87 private: 88 void ExtractSourceMapData(const std::string& allmappings, std::shared_ptr<SourceMapData>& curMapData); 89 void ExtractKeyInfo(const std::string& sourceMap, std::vector<std::string>& sourceKeyInfo); 90 std::vector<std::string> HandleMappings(const std::string& mapping); 91 bool VlqRevCode(const std::string& vStr, std::vector<int32_t>& ans); 92 MappingInfo Find(int32_t row, int32_t col, const SourceMapData& targetMap); 93 void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column); 94 std::string GetRelativePath(const std::string& sources); 95 std::string GetSourceInfo(const std::string& line, const std::string& column, const SourceMapData& targetMap); 96 97 private: 98 bool isModular_ = true; 99 std::string hapPath_; 100 std::unordered_map<std::string, std::shared_ptr<SourceMapData>> sourceMaps_; 101 std::shared_ptr<SourceMapData> nonModularMap_; 102 static ReadSourceMapCallback readSourceMapFunc_; 103 static std::mutex sourceMapMutex_; 104 static GetHapPathCallback getHapPathFunc_; 105 std::unordered_map<std::string, std::string> sources_; 106 std::unordered_map<std::string, std::string> mappings_; 107 }; 108 } // namespace JsEnv 109 } // namespace OHOS 110 111 #endif // OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H 112