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