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 ErrorPos = std::pair<uint32_t, uint32_t>; 33 struct SourceMapInfo { 34 int32_t beforeRow = 0; 35 int32_t beforeColumn = 0; 36 int32_t afterRow = 0; 37 int32_t afterColumn = 0; 38 int32_t sourcesVal = 0; 39 int32_t namesVal = 0; 40 }; 41 42 struct MappingInfo { 43 int32_t row = 0; 44 int32_t col = 0; 45 std::string sources; 46 }; 47 48 class SourceMapData final { 49 public: 50 SourceMapData() = default; 51 ~SourceMapData() = default; 52 53 SourceMapInfo nowPos_; 54 std::vector<std::string> files_; 55 std::vector<std::string> sources_; 56 std::vector<std::string> names_; 57 std::vector<std::string> mappings_; 58 std::vector<SourceMapInfo> afterPos_; 59 GetSourceMapData()60 inline SourceMapData GetSourceMapData() const 61 { 62 return *this; 63 } 64 }; 65 66 class SourceMap final { 67 public: 68 SourceMap() = default; 69 ~SourceMap() = default; 70 71 void Init(const std::string& url, const std::string& hapPath); 72 bool TranslateUrlPositionBySourceMap(std::string& url, int& line, int& column); 73 static void ExtractStackInfo(const std::string& stackStr, std::vector<std::string>& res); 74 75 private: 76 void SplitSourceMap(const std::string& url, const std::string& sourceMapData); 77 void ExtractSourceMapData(const std::string& sourceMapData, std::shared_ptr<SourceMapData>& curMapData); 78 void ExtractKeyInfo(const std::string& sourceMap, std::vector<std::string>& sourceKeyInfo); 79 std::vector<std::string> HandleMappings(const std::string& mapping); 80 bool VlqRevCode(const std::string& vStr, std::vector<int32_t>& ans); 81 MappingInfo Find(int32_t row, int32_t col, const SourceMapData& targetMap, const std::string& key); 82 void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column); 83 bool GetLineAndColumnNumbers(int& line, int& column, SourceMapData& targetMap, std::string& key); 84 bool ReadSourceMapData(const std::string& hapPath, std::string& content); 85 86 private: 87 std::unordered_map<std::string, std::shared_ptr<SourceMapData>> sourceMaps_; 88 }; 89 } // namespace panda 90 } // namespace ecmascript 91 92 #endif // OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H 93