1 /* 2 * Copyright (c) 2022 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 FOUNDATION_ABILITY_RUNTIME_SOURCE_MAP_H 17 #define FOUNDATION_ABILITY_RUNTIME_SOURCE_MAP_H 18 19 #include <cstring> 20 #include <fstream> 21 #include <limits.h> 22 #include <map> 23 #include <utility> 24 #include <thread> 25 #include <vector> 26 27 namespace panda::ecmascript { 28 class EcmaVM; 29 } // namespace panda::ecmascript 30 namespace OHOS::AbilityRuntime { 31 using ErrorPos = std::pair<uint32_t, uint32_t>; 32 using panda::ecmascript::EcmaVM; 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 ModSourceMap final { 67 public: 68 explicit ModSourceMap() = default; ModSourceMap(const bool isStageModel)69 explicit ModSourceMap(const bool isStageModel) : isStageModel(isStageModel) {}; ModSourceMap(const std::string & bundleCodeDir,const bool isStageModel)70 explicit ModSourceMap(const std::string& bundleCodeDir, const bool isStageModel) : 71 isStageModel(isStageModel), bundleCodeDir_(bundleCodeDir) {}; 72 ~ModSourceMap() = default; 73 74 static std::string TranslateBySourceMap(const std::string& stackStr, ModSourceMap& targetMaps, const std::string& BundleCodeDir); 75 static std::string GetOriginalNames(std::shared_ptr<SourceMapData> targetMapData, const std::string& sourceCode, 76 uint32_t& errorPos); 77 static ErrorPos GetErrorPos(const std::string& rawStack); 78 static void NonModularLoadSourceMap(ModSourceMap& targetMaps, const std::string& targetMap); 79 80 bool isStageModel = true; 81 82 private: 83 static void Init(const std::string& sourceMap, SourceMapData& curMap); 84 static bool GetSourceMapData(ModSourceMap& bindSourceMaps, const std::string& temp, SourceMapData& curMapData); 85 static MappingInfo Find(int32_t row, int32_t col, const SourceMapData& targetMap, const std::string& key); 86 static void ExtractKeyInfo(const std::string& sourceMap, std::vector<std::string>& sourceKeyInfo); 87 static void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column); 88 static int32_t StringToInt(const std::string& value); 89 static std::string GetRelativePath(const std::string& sources); 90 static std::string GetSourceInfo(const std::string& line, const std::string& column, const SourceMapData& targetMap, const std::string& key); 91 static bool ReadSourceMapData(const std::string& filePath, std::string& content); 92 static std::vector<std::string> HandleMappings(const std::string& mapping); 93 static uint32_t Base64CharToInt(char charCode); 94 static bool VlqRevCode(const std::string& vStr, std::vector<int32_t>& ans); 95 96 std::string bundleCodeDir_; 97 std::map<std::string, SourceMapData> sourceMaps_; 98 std::shared_ptr<SourceMapData> nonModularMap_; 99 }; 100 } // namespace OHOS::AbilityRuntime 101 102 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_SOURCE_MAP_H 103