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 <unordered_map> 23 #include <utility> 24 #include <thread> 25 #include <vector> 26 27 namespace OHOS { 28 namespace JsEnv { 29 using ErrorPos = std::pair<uint32_t, uint32_t>; 30 struct SourceMapInfo { 31 int32_t beforeRow = 0; 32 int32_t beforeColumn = 0; 33 int32_t afterRow = 0; 34 int32_t afterColumn = 0; 35 int32_t sourcesVal = 0; 36 int32_t namesVal = 0; 37 }; 38 39 struct MappingInfo { 40 int32_t row = 0; 41 int32_t col = 0; 42 std::string sources; 43 }; 44 45 class SourceMapData final { 46 public: 47 SourceMapData() = default; 48 ~SourceMapData() = default; 49 50 SourceMapInfo nowPos_; 51 std::vector<std::string> files_; 52 std::vector<std::string> sources_; 53 std::vector<std::string> names_; 54 std::vector<std::string> mappings_; 55 std::vector<SourceMapInfo> afterPos_; 56 GetSourceMapData()57 inline SourceMapData GetSourceMapData() const 58 { 59 return *this; 60 } 61 }; 62 63 using ReadSourceMapCallback = std::function<bool(const std::string& hapPath, 64 const std::string& sourceMapPath, std::string& content)>; 65 66 class SourceMap final { 67 public: 68 SourceMap() = default; 69 ~SourceMap() = default; 70 71 void Init(bool isModular, const std::string& hapPath); 72 std::string TranslateBySourceMap(const std::string& stackStr); 73 74 static std::string GetOriginalNames(std::shared_ptr<SourceMapData> targetMapData, 75 const std::string& sourceCode, uint32_t& errorPos); 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 80 private: 81 void SplitSourceMap(const std::string& sourceMapData); 82 void ExtractSourceMapData(const std::string& sourceMapData, std::shared_ptr<SourceMapData>& curMapData); 83 void ExtractStackInfo(const std::string& stackStr, std::vector<std::string>& res); 84 void ExtractKeyInfo(const std::string& sourceMap, std::vector<std::string>& sourceKeyInfo); 85 std::vector<std::string> HandleMappings(const std::string& mapping); 86 bool VlqRevCode(const std::string& vStr, std::vector<int32_t>& ans); 87 MappingInfo Find(int32_t row, int32_t col, const SourceMapData& targetMap, const std::string& key); 88 void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column); 89 std::string GetRelativePath(const std::string& sources); 90 std::string GetSourceInfo(const std::string& line, const std::string& column, 91 const SourceMapData& targetMap, const std::string& key); 92 93 private: 94 bool isModular_ = true; 95 std::string hapPath_; 96 std::unordered_map<std::string, std::shared_ptr<SourceMapData>> sourceMaps_; 97 std::shared_ptr<SourceMapData> nonModularMap_; 98 static ReadSourceMapCallback readSourceMapFunc_; 99 }; 100 } // namespace JsEnv 101 } // namespace OHOS 102 103 #endif // OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H 104