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 Clock = std::chrono::high_resolution_clock; 33 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 }; 47 48 class SourceMapData final { 49 public: 50 SourceMapData() = default; 51 ~SourceMapData() = default; 52 53 std::string url_; 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() const61 inline SourceMapData GetSourceMapData() const 62 { 63 return *this; 64 } 65 }; 66 67 class SourceMap final { 68 public: 69 SourceMap() = default; 70 ~SourceMap() = default; 71 72 #if defined(PANDA_TARGET_OHOS) 73 void Init(const std::string& hapPath); 74 #endif 75 void Init(uint8_t *data, size_t dataSize); 76 bool TranslateUrlPositionBySourceMap(std::string& url, int& line, int& column); 77 78 private: 79 void SplitSourceMap(const std::string& sourceMapData); 80 void ExtractSourceMapData(const std::string& sourceMapData, std::shared_ptr<SourceMapData>& curMapData); 81 void ExtractKeyInfo(const std::string& sourceMap, std::vector<std::string>& sourceKeyInfo); 82 std::vector<std::string> HandleMappings(const std::string& mapping); 83 bool VlqRevCode(const std::string& vStr, std::vector<int32_t>& ans); 84 MappingInfo Find(int32_t row, int32_t col, const SourceMapData& targetMap, bool& isReplaces); 85 void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column); 86 bool GetLineAndColumnNumbers(int& line, int& column, SourceMapData& targetMap, bool& isReplaces); 87 uint32_t Base64CharToInt(char charCode); 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 }; 98 } // namespace panda 99 } // namespace ecmascript 100 101 #endif // OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H 102