/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H #define OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H #include #include #include #include #include #include #include #include namespace OHOS { namespace JsEnv { namespace { const std::string NOT_FOUNDMAP = "Cannot get SourceMap info, dump raw stack:\n"; } using ErrorPos = std::pair; struct SourceMapInfo { int32_t beforeRow = 0; int32_t beforeColumn = 0; int32_t afterRow = 0; int32_t afterColumn = 0; int32_t sourcesVal = 0; int32_t namesVal = 0; }; struct MappingInfo { int32_t row = 0; int32_t col = 0; std::string sources; }; class SourceMapData final { public: SourceMapData() = default; ~SourceMapData() = default; SourceMapInfo nowPos_; std::vector files_; std::vector sources_; std::vector names_; std::vector mappings_; std::vector afterPos_; inline SourceMapData GetSourceMapData() const { return *this; } }; using ReadSourceMapCallback = std::function; using GetHapPathCallback = std::function &hapList)>; class SourceMap final { public: SourceMap() = default; ~SourceMap() = default; void Init(bool isModular, const std::string& hapPath); std::string TranslateBySourceMap(const std::string& stackStr); bool TranslateUrlPositionBySourceMap(std::string& url, int& line, int& column); static ErrorPos GetErrorPos(const std::string& rawStack); static void RegisterReadSourceMapCallback(ReadSourceMapCallback readFunc); static bool ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content); static void RegisterGetHapPathCallback(GetHapPathCallback getFunc); static void GetHapPath(const std::string &bundleName, std::vector &hapList); bool GetLineAndColumnNumbers(int& line, int& column, SourceMapData& targetMap, std::string& url); static void ExtractStackInfo(const std::string& stackStr, std::vector& res); void SplitSourceMap(const std::string& sourceMapData); private: void ExtractSourceMapData(const std::string& allmappings, std::shared_ptr& curMapData); void ExtractKeyInfo(const std::string& sourceMap, std::vector& sourceKeyInfo); std::vector HandleMappings(const std::string& mapping); bool VlqRevCode(const std::string& vStr, std::vector& ans); MappingInfo Find(int32_t row, int32_t col, const SourceMapData& targetMap); void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column); std::string GetRelativePath(const std::string& sources); std::string GetSourceInfo(const std::string& line, const std::string& column, const SourceMapData& targetMap); private: bool isModular_ = true; std::string hapPath_; std::unordered_map> sourceMaps_; std::shared_ptr nonModularMap_; static ReadSourceMapCallback readSourceMapFunc_; static std::mutex sourceMapMutex_; static GetHapPathCallback getHapPathFunc_; std::unordered_map sources_; std::unordered_map mappings_; }; } // namespace JsEnv } // namespace OHOS #endif // OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H