diff --git a/bundle.json b/bundle.json index ce49bbf3c6..a74ccc59d6 100644 --- a/bundle.json +++ b/bundle.json @@ -162,7 +162,8 @@ "header_base": "//foundation/ability/ability_runtime/js_environment/interfaces/inner_api", "header_files": [ "js_environment.h", - "js_environment_impl.h" + "js_environment_impl.h", + "source_map.h" ] }, "name": "//foundation/ability/ability_runtime/js_environment/frameworks/js_environment:js_environment" @@ -177,15 +178,6 @@ }, "name": "//foundation/ability/ability_runtime/interfaces/inner_api/runtime:runtime" }, - { - "header": { - "header_base": "//foundation/ability/ability_runtime/interfaces/inner_api/runtime/include/", - "header_files": [ - "source_map.h" - ] - }, - "name": "//foundation/ability/ability_runtime/interfaces/inner_api/runtime:source_map" - }, { "header": { "header_base": "//foundation/ability/ability_runtime/interfaces/inner_api/napi_base_context/include", diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 1886515721..10fec41bec 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -52,6 +52,7 @@ #include "systemcapability.h" #include "commonlibrary/ets_utils/js_sys_module/timer/timer.h" #include "commonlibrary/ets_utils/js_sys_module/console/console.h" +#include "source_map.h" #ifdef SUPPORT_GRAPHICS #include "declarative_module_preloader.h" @@ -67,6 +68,7 @@ constexpr uint8_t SYSCAP_MAX_SIZE = 64; constexpr int64_t DEFAULT_GC_POOL_SIZE = 0x10000000; // 256MB const std::string SANDBOX_ARK_CACHE_PATH = "/data/storage/ark-cache/"; const std::string SANDBOX_ARK_PROIFILE_PATH = "/data/storage/ark-profile"; +const std::string MEGER_SOURCE_MAP_PATH = "ets/sourceMaps.map"; #ifdef APP_USE_ARM constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib/libark_debugger.z.so"; #else @@ -625,6 +627,7 @@ void JsRuntime::InitSourceMap(const Options& options) { CHECK_POINTER(jsEnv_); jsEnv_->InitSourceMap(options.bundleCodeDir, options.isStageModel); + JsEnv::SourceMap::RegisterReadSourceMapCallback(JsRuntime::ReadSourceMapData); } void JsRuntime::Deinitialize() @@ -977,5 +980,28 @@ void JsRuntime::RegisterQuickFixQueryFunc(const std::map extractor = ExtractorUtil::GetExtractor( + ExtractorUtil::GetLoadFilePath(hapPath), newCreate); + if (extractor == nullptr) { + HILOG_ERROR("hap's path: %{public}s, get extractor failed", hapPath.c_str()); + return false; + } + std::unique_ptr dataPtr = nullptr; + size_t len = 0; + if (!extractor->ExtractToBufByName(MEGER_SOURCE_MAP_PATH, dataPtr, len)) { + HILOG_ERROR("get mergeSourceMapData fileBuffer failed"); + return false; + } + content = reinterpret_cast(dataPtr.get()); + return true; +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/interfaces/inner_api/runtime/BUILD.gn b/interfaces/inner_api/runtime/BUILD.gn index 259c7beef1..6bf20bec78 100644 --- a/interfaces/inner_api/runtime/BUILD.gn +++ b/interfaces/inner_api/runtime/BUILD.gn @@ -63,7 +63,6 @@ ohos_shared_library("runtime") { "${ability_runtime_native_path}/runtime/ohos_js_env_logger.cpp", "${ability_runtime_native_path}/runtime/ohos_js_environment_impl.cpp", "${ability_runtime_native_path}/runtime/runtime.cpp", - "${ability_runtime_native_path}/runtime/source_map.cpp", ] configs = [ ":runtime_config" ] @@ -121,28 +120,3 @@ ohos_shared_library("runtime") { subsystem_name = "ability" part_name = "ability_runtime" } - -config("source_map_config") { - include_dirs = [ "${ability_runtime_services_path}/common/include" ] -} - -config("source_map_public_config") { - include_dirs = [ "include" ] -} - -ohos_shared_library("source_map") { - sources = [ "${ability_runtime_native_path}/runtime/source_map.cpp" ] - - configs = [ ":source_map_config" ] - - public_configs = [ ":source_map_public_config" ] - - external_deps = [ - "ability_base:extractortool", - "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", - ] - - subsystem_name = "ability" - part_name = "ability_runtime" -} diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index 6ca9e43e11..b567ffdf86 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -25,7 +25,10 @@ #include "native_engine/native_engine.h" #include "runtime.h" -#include "source_map.h" + +namespace panda::ecmascript { +class EcmaVM; +} // namespace panda::ecmascript namespace OHOS { namespace AppExecFwk { class EventHandler; @@ -44,7 +47,6 @@ using AppLibPathMap = std::map>; namespace AbilityRuntime { class TimerTask; -class ModSourceMap; inline void *DetachCallbackFunc(NativeEngine *engine, void *value, void *) { @@ -60,16 +62,13 @@ public: static void SetAppLibPath(const AppLibPathMap& appLibPaths); + static bool ReadSourceMapData(const std::string& hapPath, std::string& content); + JsRuntime(); ~JsRuntime() override; NativeEngine& GetNativeEngine() const; - ModSourceMap& GetSourceMap() const - { - return *bindSourceMaps_; - } - Language GetLanguage() const override { return Language::JS; @@ -116,7 +115,6 @@ private: bool debugMode_ = false; bool preloaded_ = false; bool isBundle_ = true; - std::unique_ptr bindSourceMaps_; std::string codePath_; std::string moduleName_; std::unique_ptr methodRequireNapiRef_; diff --git a/interfaces/inner_api/runtime/include/source_map.h b/interfaces/inner_api/runtime/include/source_map.h deleted file mode 100644 index 310b7966dc..0000000000 --- a/interfaces/inner_api/runtime/include/source_map.h +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2022 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 FOUNDATION_ABILITY_RUNTIME_SOURCE_MAP_H -#define FOUNDATION_ABILITY_RUNTIME_SOURCE_MAP_H - -#include -#include -#include -#include -#include -#include -#include - -namespace panda::ecmascript { -class EcmaVM; -} // namespace panda::ecmascript -namespace OHOS::AbilityRuntime { -using ErrorPos = std::pair; -using panda::ecmascript::EcmaVM; -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; - } -}; - -class ModSourceMap final { -public: - explicit ModSourceMap() = default; - explicit ModSourceMap(const bool isStageModel) : isStageModel(isStageModel) {}; - explicit ModSourceMap(const std::string& bundleCodeDir, const bool isStageModel) : isStageModel(isStageModel), - bundleCodeDir_(bundleCodeDir) {}; - ~ModSourceMap() = default; - - static std::string TranslateBySourceMap(const std::string& stackStr, ModSourceMap& targetMaps, - const std::string& hapPath); - static std::string GetOriginalNames(std::shared_ptr targetMapData, - const std::string& sourceCode, uint32_t& errorPos); - static ErrorPos GetErrorPos(const std::string& rawStack); - static void NonModularLoadSourceMap(ModSourceMap& targetMaps, const std::string& targetMap); - - bool isStageModel = true; - -private: - static void Init(const std::string& sourceMap, SourceMapData& curMap); - static MappingInfo Find(int32_t row, int32_t col, const SourceMapData& targetMap, const std::string& key); - static void ExtractKeyInfo(const std::string& sourceMap, std::vector& sourceKeyInfo); - static void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column); - static int32_t StringToInt(const std::string& value); - static std::string GetRelativePath(const std::string& sources); - static std::string GetSourceInfo(const std::string& line, const std::string& column, - const SourceMapData& targetMap, const std::string& key); - static bool ReadSourceMapData(const std::string& hapPath, std::string& content); - static std::vector HandleMappings(const std::string& mapping); - static uint32_t Base64CharToInt(char charCode); - static bool VlqRevCode(const std::string& vStr, std::vector& ans); - - std::string bundleCodeDir_; - std::map sourceMaps_; - std::shared_ptr nonModularMap_; -}; -} // namespace OHOS::AbilityRuntime - -#endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_SOURCE_MAP_H diff --git a/interfaces/kits/native/appkit/app/main_thread.h b/interfaces/kits/native/appkit/app/main_thread.h index 5ad8bc794f..84dac42a19 100644 --- a/interfaces/kits/native/appkit/app/main_thread.h +++ b/interfaces/kits/native/appkit/app/main_thread.h @@ -37,7 +37,6 @@ #define ABILITY_LIBRARY_LOADER class Runtime; -class ModSourceMap; namespace OHOS { namespace AppExecFwk { using namespace OHOS::Global; diff --git a/js_environment/frameworks/js_environment/BUILD.gn b/js_environment/frameworks/js_environment/BUILD.gn index 06cd2a643a..cbf39e6cbf 100644 --- a/js_environment/frameworks/js_environment/BUILD.gn +++ b/js_environment/frameworks/js_environment/BUILD.gn @@ -29,6 +29,7 @@ ohos_shared_library("js_environment") { sources = [ "${utils_path}/src/js_env_logger.cpp", "src/js_environment.cpp", + "src/source_map.cpp", "src/uncaught_exception_callback.cpp", ] @@ -42,7 +43,6 @@ ohos_shared_library("js_environment") { deps = [ "${arkui_path}/napi:ace_napi_ark" ] external_deps = [ - "ability_runtime:source_map", "ets_runtime:libark_jsruntime", "napi:ace_napi", ] diff --git a/js_environment/frameworks/js_environment/src/js_environment.cpp b/js_environment/frameworks/js_environment/src/js_environment.cpp index caf83f30d3..2195e155c5 100644 --- a/js_environment/frameworks/js_environment/src/js_environment.cpp +++ b/js_environment/frameworks/js_environment/src/js_environment.cpp @@ -109,14 +109,15 @@ void JsEnvironment::RemoveTask(const std::string& name) void JsEnvironment::InitSourceMap(const std::string& bundleCodeDir, bool isStageModel) { - bindSourceMaps_ = std::make_shared(bundleCodeDir, isStageModel); + bindSourceMaps_ = std::make_shared(); } void JsEnvironment::RegisterUncaughtExceptionHandler(JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo) { if ((bindSourceMaps_ != nullptr) && (engine_ != nullptr)) { + bool isModular = !panda::JSNApi::IsBundle(vm_); engine_->RegisterUncaughtExceptionHandler(UncaughtExceptionCallback(uncaughtExceptionInfo.hapPath, - uncaughtExceptionInfo.uncaughtTask, bindSourceMaps_)); + uncaughtExceptionInfo.uncaughtTask, bindSourceMaps_, isModular)); } } diff --git a/frameworks/native/runtime/source_map.cpp b/js_environment/frameworks/js_environment/src/source_map.cpp similarity index 63% rename from frameworks/native/runtime/source_map.cpp rename to js_environment/frameworks/js_environment/src/source_map.cpp index 8fd43de2c8..d60ca98a24 100644 --- a/frameworks/native/runtime/source_map.cpp +++ b/js_environment/frameworks/js_environment/src/source_map.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -22,15 +22,11 @@ #include #include -#include "extractor.h" -#include "hilog_wrapper.h" - -using namespace OHOS::AbilityBase; -using Extractor = OHOS::AbilityBase::Extractor; +#include "js_env_logger.h" namespace OHOS { -namespace AbilityRuntime { - +namespace JsEnv { +namespace { constexpr char SOURCES[] = "sources"; constexpr char NAMES[] = "names"; constexpr char MAPPINGS[] = "mappings"; @@ -41,9 +37,7 @@ constexpr char DELIMITER_COMMA = ','; constexpr char DELIMITER_SEMICOLON = ';'; constexpr char DOUBLE_SLASH = '\\'; constexpr char WEBPACK[] = "webpack:///"; -const std::string REALPATH_FLAG = "/temprary/"; -const std::string MEGER_SOURCE_MAP_PATH = "ets/sourceMaps.map"; -const std::string NOT_FOUNDMAP = "Cannot get SourceMap info, dump raw stack:\n"; +constexpr int32_t INDEX_ONE = 1; constexpr int32_t INDEX_TWO = 2; constexpr int32_t INDEX_THREE = 3; constexpr int32_t INDEX_FOUR = 4; @@ -51,35 +45,248 @@ constexpr int32_t ANS_MAP_SIZE = 5; constexpr int32_t NUM_TWENTY = 20; constexpr int32_t NUM_TWENTYSIX = 26; constexpr int32_t DIGIT_NUM = 64; +const std::string NOT_FOUNDMAP = "Cannot get SourceMap info, dump raw stack:\n"; +} // namespace +ReadSourceMapCallback SourceMap::readSourceMapFunc_ = nullptr; -bool ModSourceMap::ReadSourceMapData(const std::string& hapPath, std::string& content) +int32_t StringToInt(const std::string& value) { - if (hapPath.empty()) { - HILOG_ERROR("hapPath is empty"); - return false; + errno = 0; + char* pEnd = nullptr; + int64_t result = std::strtol(value.c_str(), &pEnd, 10); + if (pEnd == value.c_str() || (result < INT_MIN || result > INT_MAX) || errno == ERANGE) { + return 0; + } else { + return result; } - bool newCreate = false; - std::shared_ptr extractor = ExtractorUtil::GetExtractor( - ExtractorUtil::GetLoadFilePath(hapPath), newCreate); - if (extractor == nullptr) { - HILOG_ERROR("hapPath %{public}s GetExtractor failed", hapPath.c_str()); - return false; - } - std::unique_ptr dataPtr = nullptr; - size_t len = 0; - if (!extractor->ExtractToBufByName(MEGER_SOURCE_MAP_PATH, dataPtr, len)) { - HILOG_ERROR("get mergeSourceMapData fileBuffer failed"); - return false; - } - content = reinterpret_cast(dataPtr.get()); - return true; } -MappingInfo ModSourceMap::Find(int32_t row, int32_t col, const SourceMapData& targetMap, const std::string& key) +uint32_t Base64CharToInt(char charCode) { - if (row < 1 || col < 1) { - HILOG_ERROR("the input pos is wrong"); - return MappingInfo {}; + if ('A' <= charCode && charCode <= 'Z') { + // 0 - 25: ABCDEFGHIJKLMNOPQRSTUVWXYZ + return charCode - 'A'; + } else if ('a' <= charCode && charCode <= 'z') { + // 26 - 51: abcdefghijklmnopqrstuvwxyz + return charCode - 'a' + 26; + } else if ('0' <= charCode && charCode <= '9') { + // 52 - 61: 0123456789 + return charCode - '0' + 52; + } else if (charCode == '+') { + // 62: + + return 62; + } else if (charCode == '/') { + // 63: / + return 63; + } + return DIGIT_NUM; +}; + +void SourceMap::Init(bool isModular, const std::string& sourceMap) +{ + isModular_ = isModular; + SplitSourceMap(sourceMap); +} + +std::string SourceMap::TranslateBySourceMap(const std::string& stackStr) +{ + std::string closeBrace = ")"; + std::string openBrace = "("; + std::string ans = ""; + + // find per line of stack + std::vector res; + ExtractStackInfo(stackStr, res); + + // collect error info first + bool needGetErrorPos = false; + uint32_t i = 0; + std::string codeStart = "SourceCode ("; + std::string sourceCode = ""; + if (!res.empty()) { + std::string fristLine = res[0]; + uint32_t codeStartLen = codeStart.length(); + if (fristLine.substr(0, codeStartLen).compare(codeStart) == 0) { + sourceCode = fristLine.substr(codeStartLen, fristLine.length() - codeStartLen - 1); + i = 1; // 1 means Convert from the second line + needGetErrorPos = true; + } + } + + // collect error info first + for (; i < res.size(); i++) { + std::string temp = res[i]; + size_t start = temp.find(openBrace); + size_t end = temp.find(":"); + if (end <= start) { + continue; + } + std::string key = temp.substr(start + 1, end - start - 1); + auto closeBracePos = static_cast(temp.find(closeBrace)); + auto openBracePos = static_cast(temp.find(openBrace)); + std::string line; + std::string column; + GetPosInfo(temp, closeBracePos, line, column); + if (line.empty() || column.empty()) { + JSENV_LOG_W("the stack without line info"); + break; + } + std::string sourceInfo; + if (isModular_) { + auto iter = sourceMaps_.find(key); + if (iter != sourceMaps_.end()) { + sourceInfo = GetSourceInfo(line, column, *(iter->second), key); + } + } else { + sourceInfo = GetSourceInfo(line, column, *nonModularMap_, key); + } + if (sourceInfo.empty()) { + break; + } + temp.replace(openBracePos, closeBracePos - openBracePos + 1, sourceInfo); + replace(temp.begin(), temp.end(), '\\', '/'); + ans = ans + temp + "\n"; + } + if (ans.empty()) { + return (NOT_FOUNDMAP + stackStr + "\n"); + } + return ans; +} + +void SourceMap::SplitSourceMap(const std::string& sourceMapData) +{ + if (!isModular_) { + if (!nonModularMap_) { + nonModularMap_ = std::make_shared(); + } + return ExtractSourceMapData(sourceMapData, nonModularMap_); + } + + size_t leftBracket = 0; + size_t rightBracket = 0; + std::string value; + while ((leftBracket = sourceMapData.find(": {", rightBracket)) != std::string::npos) { + std::string left = sourceMapData.substr(leftBracket); + rightBracket = sourceMapData.find("},", leftBracket); + uint32_t subLeftBracket = leftBracket; + uint32_t subRightBracket = rightBracket; + value = sourceMapData.substr(subLeftBracket + INDEX_ONE, subRightBracket - subLeftBracket + INDEX_TWO); + std::size_t sources = value.find("\"sources\": ["); + if (sources == std::string::npos) { + continue; + } + std::size_t names = value.find("],"); + if (names == std::string::npos) { + continue; + } + // Intercept the sourcemap file path as the key + std::string key = value.substr(sources + NUM_TWENTY, names - sources - NUM_TWENTYSIX); + std::shared_ptr modularMap = std::make_shared(); + ExtractSourceMapData(value, modularMap); + sourceMaps_.emplace(key, modularMap); + } +} + +void SourceMap::ExtractStackInfo(const std::string& stackStr, std::vector& res) +{ + std::string tempStr; + for (uint32_t i = 0; i < stackStr.length(); i++) { + if (stackStr[i] == '\n') { + res.push_back(tempStr); + tempStr = ""; + } else { + tempStr += stackStr[i]; + } + } + if (!tempStr.empty()) { + res.push_back(tempStr); + } +} + +void SourceMap::ExtractSourceMapData(const std::string& sourceMapData, std::shared_ptr& curMapData) +{ + std::vector sourceKey; + ExtractKeyInfo(sourceMapData, sourceKey); + + std::string mark = ""; + for (auto sourceKeyInfo : sourceKey) { + if (sourceKeyInfo == SOURCES || sourceKeyInfo == NAMES || + sourceKeyInfo == MAPPINGS || sourceKeyInfo == FILE || + sourceKeyInfo == SOURCE_CONTENT || sourceKeyInfo == SOURCE_ROOT) { + mark = sourceKeyInfo; + } else if (mark == SOURCES) { + curMapData->sources_.push_back(sourceKeyInfo); + } else if (mark == NAMES) { + curMapData->names_.push_back(sourceKeyInfo); + } else if (mark == MAPPINGS) { + curMapData->mappings_.push_back(sourceKeyInfo); + } else if (mark == FILE) { + curMapData->files_.push_back(sourceKeyInfo); + } else { + continue; + } + } + + if (curMapData->mappings_.empty()) { + return; + } + + // transform to vector for mapping easily + curMapData->mappings_ = HandleMappings(curMapData->mappings_[0]); + + // the first bit: the column after transferring. + // the second bit: the source file. + // the third bit: the row before transferring. + // the fourth bit: the column before transferring. + // the fifth bit: the variable name. + for (const auto& mapping : curMapData->mappings_) { + if (mapping == ";") { + // plus a line for each semicolon + curMapData->nowPos_.afterRow++, + curMapData->nowPos_.afterColumn = 0; + continue; + } + std::vector ans; + + if (!VlqRevCode(mapping, ans)) { + JSENV_LOG_E("decode code fail"); + return; + } + if (ans.empty()) { + JSENV_LOG_E("decode sourcemap fail, mapping: %{public}s", mapping.c_str()); + break; + } + if (ans.size() == 1) { + curMapData->nowPos_.afterColumn += ans[0]; + continue; + } + // after decode, assgin each value to the position + curMapData->nowPos_.afterColumn += ans[0]; + curMapData->nowPos_.sourcesVal += ans[INDEX_ONE]; + curMapData->nowPos_.beforeRow += ans[INDEX_TWO]; + curMapData->nowPos_.beforeColumn += ans[INDEX_THREE]; + if (ans.size() == ANS_MAP_SIZE) { + curMapData->nowPos_.namesVal += ans[INDEX_FOUR]; + } + curMapData->afterPos_.push_back({ + curMapData->nowPos_.beforeRow, + curMapData->nowPos_.beforeColumn, + curMapData->nowPos_.afterRow, + curMapData->nowPos_.afterColumn, + curMapData->nowPos_.sourcesVal, + curMapData->nowPos_.namesVal + }); + } + curMapData->mappings_.clear(); + curMapData->mappings_.shrink_to_fit(); + sourceKey.clear(); + sourceKey.shrink_to_fit(); +} + +MappingInfo SourceMap::Find(int32_t row, int32_t col, const SourceMapData& targetMap, const std::string& key) +{ + if (row < 1 || col < 1 || targetMap.afterPos_.empty()) { + return MappingInfo {0, 0, ""}; } row--; col--; @@ -113,7 +320,7 @@ MappingInfo ModSourceMap::Find(int32_t row, int32_t col, const SourceMapData& ta }; } -void ModSourceMap::ExtractKeyInfo(const std::string& sourceMap, std::vector& sourceKeyInfo) +void SourceMap::ExtractKeyInfo(const std::string& sourceMap, std::vector& sourceKeyInfo) { uint32_t cnt = 0; std::string tempStr; @@ -140,7 +347,7 @@ void ModSourceMap::ExtractKeyInfo(const std::string& sourceMap, std::vector INT_MAX) || errno == ERANGE) { - return 0; - } else { - return result; - } -} - -std::string ModSourceMap::GetRelativePath(const std::string& sources) +std::string SourceMap::GetRelativePath(const std::string& sources) { std::string temp = sources; std::size_t splitPos = std::string::npos; @@ -193,86 +388,7 @@ std::string ModSourceMap::GetRelativePath(const std::string& sources) return sources; } -void ModSourceMap::Init(const std::string& sourceMap, SourceMapData& curMapData) -{ - std::vector sourceKeyInfo; - std::string mark = ""; - - ExtractKeyInfo(sourceMap, sourceKeyInfo); - - // first: find the key info and record the temp key info - // second: add the detail into the keyinfo - for (auto keyInfo : sourceKeyInfo) { - if (keyInfo == SOURCES || keyInfo == NAMES || keyInfo == MAPPINGS || keyInfo == FILE || - keyInfo == SOURCE_CONTENT || keyInfo == SOURCE_ROOT) { - // record the temp key info - mark = keyInfo; - } else if (mark == SOURCES) { - curMapData.sources_.push_back(keyInfo); - } else if (mark == NAMES) { - curMapData.names_.push_back(keyInfo); - } else if (mark == MAPPINGS) { - curMapData.mappings_.push_back(keyInfo); - } else if (mark == FILE) { - curMapData.files_.push_back(keyInfo); - } else { - continue; - } - } - - // transform to vector for mapping easily - curMapData.mappings_ = HandleMappings(curMapData.mappings_[0]); - - // the first bit: the column after transferring. - // the second bit: the source file. - // the third bit: the row before transferring. - // the fourth bit: the column before transferring. - // the fifth bit: the variable name. - for (const auto& mapping : curMapData.mappings_) { - if (mapping == ";") { - // plus a line for each semicolon - curMapData.nowPos_.afterRow++, - curMapData.nowPos_.afterColumn = 0; - continue; - } - // decode each mapping ";QAABC" - std::vector ans; - if (!VlqRevCode(mapping, ans)) { - HILOG_ERROR("decode code fail"); - return; - } - if (ans.size() == 0) { - HILOG_ERROR("decode sourcemap fail, mapping: %{public}s", mapping.c_str()); - break; - } - if (ans.size() == 1) { - curMapData.nowPos_.afterColumn += ans[0]; - continue; - } - // after decode, assgin each value to the position - curMapData.nowPos_.afterColumn += ans[0]; - curMapData.nowPos_.sourcesVal += ans[1]; - curMapData.nowPos_.beforeRow += ans[INDEX_TWO]; - curMapData.nowPos_.beforeColumn += ans[INDEX_THREE]; - if (ans.size() == ANS_MAP_SIZE) { - curMapData.nowPos_.namesVal += ans[INDEX_FOUR]; - } - curMapData.afterPos_.push_back({ - curMapData.nowPos_.beforeRow, - curMapData.nowPos_.beforeColumn, - curMapData.nowPos_.afterRow, - curMapData.nowPos_.afterColumn, - curMapData.nowPos_.sourcesVal, - curMapData.nowPos_.namesVal - }); - } - curMapData.mappings_.clear(); - curMapData.mappings_.shrink_to_fit(); - sourceKeyInfo.clear(); - sourceKeyInfo.shrink_to_fit(); -}; - -std::vector ModSourceMap::HandleMappings(const std::string& mapping) +std::vector SourceMap::HandleMappings(const std::string& mapping) { std::vector keyInfo; std::string tempStr; @@ -296,28 +412,7 @@ std::vector ModSourceMap::HandleMappings(const std::string& mapping return keyInfo; }; -uint32_t ModSourceMap::Base64CharToInt(char charCode) -{ - if ('A' <= charCode && charCode <= 'Z') { - // 0 - 25: ABCDEFGHIJKLMNOPQRSTUVWXYZ - return charCode - 'A'; - } else if ('a' <= charCode && charCode <= 'z') { - // 26 - 51: abcdefghijklmnopqrstuvwxyz - return charCode - 'a' + 26; - } else if ('0' <= charCode && charCode <= '9') { - // 52 - 61: 0123456789 - return charCode - '0' + 52; - } else if (charCode == '+') { - // 62: + - return 62; - } else if (charCode == '/') { - // 63: / - return 63; - } - return DIGIT_NUM; -}; - -bool ModSourceMap::VlqRevCode(const std::string& vStr, std::vector& ans) +bool SourceMap::VlqRevCode(const std::string& vStr, std::vector& ans) { const int32_t VLQ_BASE_SHIFT = 5; // binary: 100000 @@ -353,113 +448,7 @@ bool ModSourceMap::VlqRevCode(const std::string& vStr, std::vector& ans return true; }; -std::string ModSourceMap::TranslateBySourceMap(const std::string& stackStr, ModSourceMap& bindSourceMaps, - const std::string& hapPath) -{ - const std::string closeBrace = ")"; - const std::string openBrace = "("; - std::string ans = ""; - std::string tempStack = stackStr; - - // find per line of stack - std::vector res; - std::string tempStr = ""; - for (uint32_t i = 0; i < tempStack.length(); i++) { - if (tempStack[i] == '\n') { - res.push_back(tempStr); - tempStr = ""; - } else { - tempStr += tempStack[i]; - } - } - if (!tempStr.empty()) { - res.push_back(tempStr); - } - - // collect error info first - bool needGetErrorPos = false; - uint32_t i = 0; - std::string codeStart = "SourceCode ("; - std::string sourceCode = ""; - if (res.size() >= 1) { - std::string fristLine = res[0]; - uint32_t codeStartLen = codeStart.length(); - if (fristLine.substr(0, codeStartLen).compare(codeStart) == 0) { - sourceCode = fristLine.substr(codeStartLen, fristLine.length() - codeStartLen - 1); - i = 1; // 1 means Convert from the second line - needGetErrorPos = true; - } - } - std::string curSourceMap; - if (!ReadSourceMapData(hapPath, curSourceMap)) { - HILOG_ERROR("ReadSourceMapData fail"); - return stackStr; - } - std::size_t s = 0; - std::size_t j = 0; - std::string value; - std::string key; - std::map MapData; - while ((s = curSourceMap.find(": {", j)) != std::string::npos) { - j = curSourceMap.find("},", s); - uint32_t q = s; - uint32_t jj = j; - value = curSourceMap.substr(q + 1, jj - q + INDEX_TWO); - size_t sources = value.find("\"sources\": ["); - size_t names = value.find("],"); - key = value.substr(sources + NUM_TWENTY, names - sources - NUM_TWENTYSIX); - MapData.insert(std::pair(key, value)); - } - - for (; i < res.size(); i++) { - std::string temp = res[i]; - size_t start = temp.find("("); - size_t end = temp.find(":"); - std::string key = temp.substr(start + 1, end - start - 1); - int32_t closeBracePos = static_cast(temp.find(closeBrace)); - int32_t openBracePos = static_cast(temp.find(openBrace)); - - std::string line = ""; - std::string column = ""; - GetPosInfo(temp, closeBracePos, line, column); - if (needGetErrorPos) { - needGetErrorPos = false; - } - if (line.empty() || column.empty()) { - break; - } - static SourceMapData curMapData; - if (!bindSourceMaps.isStageModel) { - if (i == 1) { // The non module scenario initializes curmapdata only at the first traversal - if (!bindSourceMaps.nonModularMap_) { - return NOT_FOUNDMAP + stackStr; - } - curMapData = *bindSourceMaps.nonModularMap_; - } - } else { - auto iter = MapData.find(key); - if (iter != MapData.end()) { - Init(iter->second, curMapData); - } else { - ans += NOT_FOUNDMAP + temp + "\n"; - continue; - } - } - const std::string sourceInfo = GetSourceInfo(line, column, curMapData, key); - if (sourceInfo.empty()) { - break; - } - temp.replace(openBracePos, closeBracePos - openBracePos + 1, sourceInfo); - ans += temp + "\n"; - } - - if (ans.empty()) { - return tempStack; - } - return ans; -} - -std::string ModSourceMap::GetSourceInfo(const std::string& line, const std::string& column, +std::string SourceMap::GetSourceInfo(const std::string& line, const std::string& column, const SourceMapData& targetMap, const std::string& key) { int32_t offSet = 0; @@ -478,15 +467,27 @@ std::string ModSourceMap::GetSourceInfo(const std::string& line, const std::stri return sourceInfo; } -void ModSourceMap::NonModularLoadSourceMap(ModSourceMap& targetMaps, const std::string& targetMap) +ErrorPos SourceMap::GetErrorPos(const std::string& rawStack) { - if (!targetMaps.nonModularMap_) { - targetMaps.nonModularMap_ = std::make_shared(); + size_t findLineEnd = rawStack.find("\n"); + if (findLineEnd == std::string::npos) { + return std::make_pair(0, 0); } - Init(targetMap, *targetMaps.nonModularMap_); + int32_t lineEnd = findLineEnd - 1; + if (lineEnd < 1 || rawStack[lineEnd - 1] == '?') { + return std::make_pair(0, 0); + } + + uint32_t secondPos = rawStack.rfind(':', lineEnd); + uint32_t fristPos = rawStack.rfind(':', secondPos - 1); + + std::string lineStr = rawStack.substr(fristPos + 1, secondPos - 1 - fristPos); + std::string columnStr = rawStack.substr(secondPos + 1, lineEnd - 1 - secondPos); + + return std::make_pair(StringToInt(lineStr), StringToInt(columnStr)); } -std::string ModSourceMap::GetOriginalNames(std::shared_ptr targetMapData, +std::string SourceMap::GetOriginalNames(std::shared_ptr targetMapData, const std::string& sourceCode, uint32_t& errorPos) { if (sourceCode.empty() || sourceCode.find("SourceCode:\n") == std::string::npos) { @@ -494,7 +495,7 @@ std::string ModSourceMap::GetOriginalNames(std::shared_ptr target } std::vector names = targetMapData->names_; if (names.size() % INDEX_TWO != 0) { - HILOG_ERROR("Names in sourcemap is wrong."); + JSENV_LOG_E("Names in sourcemap is wrong."); return sourceCode; } @@ -516,14 +517,14 @@ std::string ModSourceMap::GetOriginalNames(std::shared_ptr target } auto lineBreakPos = jsCode.rfind('\n', jsCode.length() - 2); if (lineBreakPos == std::string::npos) { - HILOG_WARN("There is something wrong in source code of summaryBody."); + JSENV_LOG_W("There is something wrong in source code of summaryBody."); return jsCode; } // adjust position of ^ in dump file if (posDiff < 0) { int32_t flagPos = static_cast(lineBreakPos) + static_cast(errorPos); if (lineBreakPos > 0 && errorPos > 0 && flagPos < 0) { - HILOG_WARN("Add overflow of sourceCode."); + JSENV_LOG_W("Add overflow of sourceCode."); return jsCode; } if (flagPos < static_cast(jsCode.length()) && jsCode[flagPos] == '^' && flagPos + posDiff - 1 > 0) { @@ -537,24 +538,17 @@ std::string ModSourceMap::GetOriginalNames(std::shared_ptr target return jsCode; } -ErrorPos ModSourceMap::GetErrorPos(const std::string& rawStack) +void SourceMap::RegisterReadSourceMapCallback(ReadSourceMapCallback readFunc) { - size_t findLineEnd = rawStack.find("\n"); - if (findLineEnd == std::string::npos) { - return std::make_pair(0, 0); - } - int32_t lineEnd = findLineEnd - 1; - if (lineEnd < 1 || rawStack[lineEnd - 1] == '?') { - return std::make_pair(0, 0); - } - - uint32_t secondPos = rawStack.rfind(':', lineEnd); - uint32_t fristPos = rawStack.rfind(':', secondPos - 1); - - std::string lineStr = rawStack.substr(fristPos + 1, secondPos - 1 - fristPos); - std::string columnStr = rawStack.substr(secondPos + 1, lineEnd - 1 - secondPos); - - return std::make_pair(StringToInt(lineStr), StringToInt(columnStr)); + readSourceMapFunc_ = readFunc; } -} // namespace AbilityRuntime + +bool SourceMap::ReadSourceMapData(const std::string& hapPath, std::string& content) +{ + if (readSourceMapFunc_) { + return readSourceMapFunc_(hapPath, content); + } + return false; +} +} // namespace JsEnv } // namespace OHOS diff --git a/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp b/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp index 7b25b77f4b..5fce5b96cf 100644 --- a/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp +++ b/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp @@ -69,7 +69,7 @@ void UncaughtExceptionCallback::operator()(NativeValue* value) JSENV_LOG_E("errorStack is empty"); return; } - auto errorPos = AbilityRuntime::ModSourceMap::GetErrorPos(errorStack); + auto errorPos = SourceMap::GetErrorPos(errorStack); std::string error; if (obj != nullptr) { NativeValue* value = obj->GetProperty("errorfunc"); @@ -78,8 +78,11 @@ void UncaughtExceptionCallback::operator()(NativeValue* value) error = fuc->GetSourceCodeInfo(errorPos); } } - summary += error + "Stacktrace:\n" + - AbilityRuntime::ModSourceMap::TranslateBySourceMap(errorStack, *bindSourceMaps_, hapPath_); + + std::string sourceMapData; + SourceMap::ReadSourceMapData(hapPath_, sourceMapData); + bindSourceMaps_->Init(isModular_, sourceMapData); + summary += error + "Stacktrace:\n" + bindSourceMaps_->TranslateBySourceMap(errorStack); if (uncaughtTask_) { uncaughtTask_(summary, errorObj); } diff --git a/js_environment/interfaces/inner_api/js_environment.h b/js_environment/interfaces/inner_api/js_environment.h index b2b1361fcb..80fe919b8c 100644 --- a/js_environment/interfaces/inner_api/js_environment.h +++ b/js_environment/interfaces/inner_api/js_environment.h @@ -68,7 +68,7 @@ private: std::unique_ptr impl_ = nullptr; NativeEngine* engine_ = nullptr; panda::ecmascript::EcmaVM* vm_ = nullptr; - std::shared_ptr bindSourceMaps_; + std::shared_ptr bindSourceMaps_; }; } // namespace JsEnv } // namespace OHOS diff --git a/js_environment/interfaces/inner_api/source_map.h b/js_environment/interfaces/inner_api/source_map.h new file mode 100644 index 0000000000..556433b01a --- /dev/null +++ b/js_environment/interfaces/inner_api/source_map.h @@ -0,0 +1,101 @@ +/* + * 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 + +namespace OHOS { +namespace JsEnv { +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; + +class SourceMap final { +public: + SourceMap() = default; + ~SourceMap() = default; + + void Init(bool isModular, const std::string& sourceMap); + std::string TranslateBySourceMap(const std::string& stackStr); + + static std::string GetOriginalNames(std::shared_ptr targetMapData, + const std::string& sourceCode, uint32_t& errorPos); + static ErrorPos GetErrorPos(const std::string& rawStack); + static void RegisterReadSourceMapCallback(ReadSourceMapCallback readFunc); + static bool ReadSourceMapData(const std::string& hapPath, std::string& content); + +private: + void SplitSourceMap(const std::string& sourceMapData); + void ExtractSourceMapData(const std::string& sourceMapData, std::shared_ptr& curMapData); + void ExtractStackInfo(const std::string& stackStr, std::vector& res); + 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, const std::string& key); + 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, const std::string& key); + +private: + bool isModular_ = true; + std::unordered_map> sourceMaps_; + std::shared_ptr nonModularMap_; + static ReadSourceMapCallback readSourceMapFunc_; +}; +} // namespace JsEnv +} // namespace OHOS + +#endif // OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_H diff --git a/js_environment/interfaces/inner_api/uncaught_exception_callback.h b/js_environment/interfaces/inner_api/uncaught_exception_callback.h index deb5edd582..164b8df5cc 100644 --- a/js_environment/interfaces/inner_api/uncaught_exception_callback.h +++ b/js_environment/interfaces/inner_api/uncaught_exception_callback.h @@ -44,8 +44,8 @@ class UncaughtExceptionCallback final { public: UncaughtExceptionCallback(const std::string hapPath, std::function uncaughtTask, - std::shared_ptr bindSourceMaps) - : hapPath_(hapPath), uncaughtTask_(uncaughtTask), bindSourceMaps_(bindSourceMaps) + std::shared_ptr bindSourceMaps, bool isModular) + : hapPath_(hapPath), uncaughtTask_(uncaughtTask), bindSourceMaps_(bindSourceMaps), isModular_(isModular) {} UncaughtExceptionCallback() = default; @@ -56,7 +56,8 @@ public: private: std::string hapPath_; std::function uncaughtTask_; - std::shared_ptr bindSourceMaps_; + std::shared_ptr bindSourceMaps_; + bool isModular_ = false; }; } // namespace JsEnv } // namespace OHOS diff --git a/js_environment/test/unittest/BUILD.gn b/js_environment/test/unittest/BUILD.gn index 257ac46252..68d637e8a9 100644 --- a/js_environment/test/unittest/BUILD.gn +++ b/js_environment/test/unittest/BUILD.gn @@ -20,5 +20,6 @@ group("unittest") { deps = [ "js_env_log_unit_test:unittest", "js_environment_test:unittest", + "source_map_test:unittest", ] } diff --git a/test/unittest/frameworks_kits_runtime_native_test/BUILD.gn b/js_environment/test/unittest/source_map_test/BUILD.gn similarity index 69% rename from test/unittest/frameworks_kits_runtime_native_test/BUILD.gn rename to js_environment/test/unittest/source_map_test/BUILD.gn index 8c2ba5d2cd..b418d9267c 100644 --- a/test/unittest/frameworks_kits_runtime_native_test/BUILD.gn +++ b/js_environment/test/unittest/source_map_test/BUILD.gn @@ -14,7 +14,7 @@ import("//build/test.gni") import("//foundation/ability/ability_runtime/ability_runtime.gni") -module_output_path = "ability_runtime/ability_test" +module_output_path = "ability_runtime/js_environment" ohos_unittest("source_map_test") { module_out_path = module_output_path @@ -23,27 +23,19 @@ ohos_unittest("source_map_test") { # configs = [ ":module_private_config" ] deps = [ - "${ability_runtime_innerkits_path}/ability_manager:ability_manager", - "${ability_runtime_native_path}/ability/native:abilitykit_native", - "${ability_runtime_native_path}/appkit:app_context", - "${distributedschedule_path}/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", - "${global_path}/resource_management/frameworks/resmgr:global_resmgr", + "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", ] external_deps = [ + "ability_runtime:js_environment", "ability_base:want", - "ability_runtime:runtime", "bundle_framework:appexecfwk_base", "c_utils:utils", "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", ] - - if (ability_runtime_graphics) { - external_deps += [ "input:libmmi-client" ] - } } group("unittest") { diff --git a/js_environment/test/unittest/source_map_test/source_map_test.cpp b/js_environment/test/unittest/source_map_test/source_map_test.cpp new file mode 100644 index 0000000000..c13cb238e6 --- /dev/null +++ b/js_environment/test/unittest/source_map_test/source_map_test.cpp @@ -0,0 +1,394 @@ +/* + * Copyright (c) 2022 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. + */ + +#include + +#define private public +#define protected public +#include "source_map.h" +#undef private +#undef protected + +namespace OHOS { +namespace JsEnv { +using namespace testing::ext; + +class SourceMapTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void SourceMapTest::SetUpTestCase(void) +{ +} + +void SourceMapTest::TearDownTestCase(void) +{ +} + +void SourceMapTest::SetUp(void) +{ +} + +void SourceMapTest::TearDown(void) +{ +} + +bool ReadSourceMapData(const std::string& hapPath, std::string& content) +{ + if (hapPath.empty()) { + return false; + } + content = "abc"; + return true; +} + +/** + * @tc.name: JsEnv_SourceMap_0100 + * @tc.type: FUNC + * @tc.desc: Test get original names from sourceCode which is empty string. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0100 start"; + std::string sourceCode = "123"; + uint32_t errorPos = 0; + std::shared_ptr targetMapData = std::make_shared(); + targetMapData->names_.emplace_back("abc.ets"); + + std::string result = SourceMap::GetOriginalNames(targetMapData, sourceCode, errorPos); + EXPECT_EQ(result, sourceCode); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0100 end"; +} + +/** + * @tc.name: JsEnv_SourceMap_0200 + * @tc.type: FUNC + * @tc.desc: Test get original names from sourceCode which has "SourceCode:\n". + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0200 start"; + std::string sourceCode = "SourceCode:\n/pages/Index.ets:111:13"; + uint32_t errorPos = 0; + std::shared_ptr targetMapData = std::make_shared(); + targetMapData->names_.emplace_back("Index.js"); + targetMapData->names_.emplace_back("Index.ets"); + + std::string result = SourceMap::GetOriginalNames(targetMapData, sourceCode, errorPos); + EXPECT_EQ(result, sourceCode); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0200 end" << result; +} + +/** + * @tc.number: JsEnv_SourceMap_0300 + * @tc.name: GetErrorPos + * @tc.desc: Verifying GetErrorPos succeeded. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_0300, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0300 start"; + std::string rawStack = "at AssertException (/mnt/assets/ets/TestAbility/TestAbility_.js:5779:5)\n"; + auto pos = SourceMap::GetErrorPos(rawStack); + EXPECT_EQ(pos.first, 5779); + EXPECT_EQ(pos.second, 5); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0300 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_0400 + * @tc.name: GetErrorPos + * @tc.desc: Verifying GetErrorPos succeeded. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_0400, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0400 start"; + std::string rawStack = "\n"; + auto pos = SourceMap::GetErrorPos(rawStack); + EXPECT_EQ(pos.first, 0); + EXPECT_EQ(pos.second, 0); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0400 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_0500 + * @tc.name: GetErrorPos + * @tc.desc: Verifying GetErrorPos succeeded. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_0500, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0500 start"; + std::string rawStack = "?\n"; + auto pos = SourceMap::GetErrorPos(rawStack); + EXPECT_EQ(pos.first, 0); + EXPECT_EQ(pos.second, 0); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0500 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_0600 + * @tc.name: ReadSourceMapData + * @tc.desc: Verifying ReadSourceMapData Failed. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_0600, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0600 start"; + auto modSourceMap = std::make_shared(); + std::string filePath = "./abc.map"; + std::string context; + EXPECT_FALSE(modSourceMap->ReadSourceMapData(filePath, context)); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0600 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_0700 + * @tc.name: ReadSourceMapData + * @tc.desc: Verifying ReadSourceMapData succeeded. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_0700, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0700 start"; + SourceMap::RegisterReadSourceMapCallback(ReadSourceMapData); + auto modSourceMap = std::make_shared(); + std::string filePath = "./abc.map"; + std::string context; + modSourceMap->ReadSourceMapData(filePath, context); + EXPECT_STREQ(context.c_str(), "abc"); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0700 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_0800 + * @tc.name: Find + * @tc.desc: Verifying Find succeeded. + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_0800, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0800 start"; + auto modSourceMap = std::make_shared(); + int32_t row = 0; + int32_t col = 1; + SourceMapData targetMap; + std::string key = ""; + auto info = modSourceMap->Find(row, col, targetMap, key); + EXPECT_TRUE(info.sources.empty()); + + row = 1; + col = 0; + info = modSourceMap->Find(row, col, targetMap, key); + EXPECT_TRUE(info.sources.empty()); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0800 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_0900 + * @tc.name: Find + * @tc.desc: Verifying Find succeeded. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_0900, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0900 start"; + auto modSourceMap = std::make_shared(); + int32_t row = 3; + int32_t col = 3; + SourceMapData targetMap; + targetMap.files_.emplace_back("file"); + + SourceMapInfo mapInfo; + mapInfo.beforeRow = 0; + mapInfo.beforeColumn = 0; + mapInfo.afterRow = 1; + mapInfo.afterColumn = 0; + mapInfo.sourcesVal = 0; + mapInfo.namesVal = 0; + targetMap.afterPos_.emplace_back(mapInfo); + std::string key = ""; + auto info = modSourceMap->Find(row, col, targetMap, key); + EXPECT_STREQ(info.sources.c_str(), "file"); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_0900 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_1100 + * @tc.name: Find + * @tc.desc: Verify binary search. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_1100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_1100 start"; + auto modSourceMap = std::make_shared(); + int32_t row = 3; + int32_t col = 3; + SourceMapData targetMap; + targetMap.files_.emplace_back("file"); + + for (int32_t i = 0; i < 10; i++) { + for (int32_t j = 0; j < 5; j++) { + SourceMapInfo mapInfo; + mapInfo.beforeRow = 0; + mapInfo.beforeColumn = 0; + mapInfo.afterRow = i; + mapInfo.afterColumn = j; + targetMap.afterPos_.emplace_back(mapInfo); + } + } + + std::string key = ""; + auto info = modSourceMap->Find(row, col, targetMap, key); + EXPECT_EQ(info.row, 1); + EXPECT_EQ(info.col, 1); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_1100 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_1200 + * @tc.name: Find + * @tc.desc: Verify binary search. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_1200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_1200 start"; + auto modSourceMap = std::make_shared(); + int32_t row = 7; + int32_t col = 1; + SourceMapData targetMap; + targetMap.files_.emplace_back("file"); + + for (int32_t i = 0; i < 10; i++) { + SourceMapInfo mapInfo; + mapInfo.beforeRow = 0; + mapInfo.beforeColumn = 0; + mapInfo.afterRow = i; + mapInfo.afterColumn = 1; + targetMap.afterPos_.emplace_back(mapInfo); + } + + std::string key = "aaawebpack:///bbb"; + auto info = modSourceMap->Find(row, col, targetMap, key); + EXPECT_EQ(info.row, 1); + EXPECT_EQ(info.col, 1); + EXPECT_STREQ(info.sources.c_str(), "aaabbb"); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_1200 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_1300 + * @tc.name: GetPosInfo + * @tc.desc: Verifying GetPosInfo succeeded. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_1300, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_1300 start"; + auto modSourceMap = std::make_shared(); + + std::string temp = "TestAbility.js:5779:5"; + int32_t start = 22; + std::string line; + std::string column; + modSourceMap->GetPosInfo(temp, start, line, column); + EXPECT_STREQ(line.c_str(), "5779"); + EXPECT_STREQ(column.c_str(), "5"); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_1300 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_1400 + * @tc.name: GetRelativePath + * @tc.desc: Verifying GetRelativePath succeeded. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_1400, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_1400 start"; + auto modSourceMap = std::make_shared(); + + std::string sources = "TEST:/data/app/MainAbility.js"; + auto res = modSourceMap->GetRelativePath(sources); + EXPECT_STREQ(res.c_str(), "/data/app/MainAbility.js"); + GTEST_LOG_(INFO) << "JsEnv_SourceMap_1400 end"; +} + +/** + * @tc.number: JsEnv_SourceMap_1500 + * @tc.name: GetPosInfo + * @tc.desc: Verifying GetPosInfo succeeded. + * @tc.require: #I6T4K1 + */ +HWTEST_F(SourceMapTest, JsEnv_SourceMap_1500, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) << "JsEnv_SourceMap_1500 start"; + std::string sourceMaps = "{" + "\"entry/src/main/ets/pages/Index.ets\": {" + "\"version\": 3," + "\"file\": \"Index.ets\"," + "\"sourceRoot\": \"\"," + "\"sources\": [" + "\"entry/src/main/ets/pages/Index.ets\"" + "]," + "\"names\": []," + "\"mappings\": \"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" + "}" + "}"; + std::string stackStr = "entry/src/main/ets/pages/Index.ets:111:13"; + + auto mapObj = std::make_shared(); + mapObj->Init(true, sourceMaps); + std::string stack = mapObj->TranslateBySourceMap(stackStr); + EXPECT_STREQ(stack.c_str(), "entry/src/main/ets/pages/Index.ets:111:13"); + + GTEST_LOG_(INFO) << "JsEnv_SourceMap_1500 end" << stack.c_str(); +} +} // namespace AppExecFwk +} // namespace OHOS diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 03174ae29b..012f49902f 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -360,7 +360,6 @@ group("unittest") { "frameworks_kits_ability_ability_runtime_test:unittest", "frameworks_kits_ability_native_test:unittest", "frameworks_kits_appkit_native_test:unittest", - "frameworks_kits_runtime_native_test:unittest", "free_install_manager_test:unittest", "image_info_test:unittest", "implicit_start_processor_test:unittest", diff --git a/test/unittest/frameworks_kits_runtime_native_test/source_map_test.cpp b/test/unittest/frameworks_kits_runtime_native_test/source_map_test.cpp deleted file mode 100644 index 15db847ab0..0000000000 --- a/test/unittest/frameworks_kits_runtime_native_test/source_map_test.cpp +++ /dev/null @@ -1,431 +0,0 @@ -/* - * Copyright (c) 2022 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. - */ - -#include - -#define private public -#define protected public -#include "source_map.h" -#undef private -#undef protected - -namespace OHOS { -namespace AppExecFwk { -using namespace testing::ext; -using namespace OHOS; -using namespace OHOS::AppExecFwk; -using namespace OHOS::AbilityRuntime; - -class SourceMapTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void SourceMapTest::SetUpTestCase(void) -{ -} - -void SourceMapTest::TearDownTestCase(void) -{ -} - -void SourceMapTest::SetUp(void) -{ -} - -void SourceMapTest::TearDown(void) -{ -} - -/** - * @tc.number: AaFwk_SourceMap_0100 - * @tc.name: Base64CharToInt - * @tc.desc: Verify int values from A to Z. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_0100, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0100 start"; - auto modSourceMap = std::make_shared(); - char charCode = 'A'; - uint32_t value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 0); - - charCode = 'Z'; - value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 25); - - charCode = 'C'; - value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 2); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0100 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_0200 - * @tc.name: Base64CharToInt - * @tc.desc: Verify int values from a to z. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_0200, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0200 start"; - auto modSourceMap = std::make_shared(); - char charCode = 'a'; - uint32_t value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 26); - - charCode = 'z'; - value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 51); - - charCode = 'c'; - value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 28); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0200 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_0300 - * @tc.name: Base64CharToInt - * @tc.desc: Verify int values from 0 to 9. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_0300, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0300 start"; - auto modSourceMap = std::make_shared(); - char charCode = '0'; - uint32_t value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 52); - - charCode = '9'; - value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 61); - - charCode = '2'; - value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 54); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0300 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_0400 - * @tc.name: Base64CharToInt - * @tc.desc: Verify int values for + and / or other symbols. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_0400, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0400 start"; - auto modSourceMap = std::make_shared(); - char charCode = '+'; - uint32_t value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 62); - - charCode = '/'; - value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 63); - - charCode = '&'; - value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 64); - - charCode = '@'; - value = modSourceMap->Base64CharToInt(charCode); - EXPECT_EQ(static_cast(value), 64); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0400 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_0500 - * @tc.name: GetErrorPos - * @tc.desc: Verifying GetErrorPos succeeded. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_0500, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0500 start"; - auto modSourceMap = std::make_shared(); - std::string rawStack = "at AssertException (/mnt/assets/ets/TestAbility/TestAbility_.js:5779:5)\n"; - auto pos = modSourceMap->GetErrorPos(rawStack); - EXPECT_EQ(pos.first, 5779); - EXPECT_EQ(pos.second, 5); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0500 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_0600 - * @tc.name: GetErrorPos - * @tc.desc: Verifying GetErrorPos succeeded. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_0600, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0600 start"; - auto modSourceMap = std::make_shared(); - std::string rawStack = "\n"; - auto pos = modSourceMap->GetErrorPos(rawStack); - EXPECT_EQ(pos.first, 0); - EXPECT_EQ(pos.second, 0); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0600 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_0700 - * @tc.name: GetErrorPos - * @tc.desc: Verifying GetErrorPos succeeded. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_0700, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0700 start"; - auto modSourceMap = std::make_shared(); - std::string rawStack = "?\n"; - auto pos = modSourceMap->GetErrorPos(rawStack); - EXPECT_EQ(pos.first, 0); - EXPECT_EQ(pos.second, 0); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0700 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_0800 - * @tc.name: ReadSourceMapData - * @tc.desc: Verifying ReadSourceMapData Failed. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_0800, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0800 start"; - auto modSourceMap = std::make_shared(); - std::string filePath = "./source_map_test"; - std::string context; - EXPECT_FALSE(modSourceMap->ReadSourceMapData(filePath, context)); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0800 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_0900 - * @tc.name: ReadSourceMapData - * @tc.desc: Verifying ReadSourceMapData succeeded. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_0900, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0900 start"; - auto modSourceMap = std::make_shared(); - std::string filePath = "./abc.map"; - std::string context; - modSourceMap->ReadSourceMapData(filePath, context); - EXPECT_TRUE(context.empty()); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_0900 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_1000 - * @tc.name: Find - * @tc.desc: Verifying Find succeeded. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_1000, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1000 start"; - auto modSourceMap = std::make_shared(); - int32_t row = 0; - int32_t col = 1; - SourceMapData targetMap; - std::string key = ""; - auto info = modSourceMap->Find(row, col, targetMap, key); - EXPECT_TRUE(info.sources.empty()); - - row = 1; - col = 0; - info = modSourceMap->Find(row, col, targetMap, key); - EXPECT_TRUE(info.sources.empty()); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1000 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_1100 - * @tc.name: Find - * @tc.desc: Verifying Find succeeded. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_1100, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1100 start"; - auto modSourceMap = std::make_shared(); - int32_t row = 0; - int32_t col = 1; - SourceMapData targetMap; - std::string key = ""; - auto info = modSourceMap->Find(row, col, targetMap, key); - EXPECT_TRUE(info.sources.empty()); - - row = 1; - col = 0; - info = modSourceMap->Find(row, col, targetMap, key); - EXPECT_TRUE(info.sources.empty()); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1100 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_1200 - * @tc.name: Find - * @tc.desc: Verifying Find succeeded. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_1200, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1200 start"; - auto modSourceMap = std::make_shared(); - int32_t row = 3; - int32_t col = 3; - SourceMapData targetMap; - targetMap.files_.emplace_back("file"); - - SourceMapInfo mapInfo; - mapInfo.beforeRow = 0; - mapInfo.beforeColumn = 0; - mapInfo.afterRow = 1; - mapInfo.afterColumn = 0; - mapInfo.sourcesVal = 0; - mapInfo.namesVal = 0; - targetMap.afterPos_.emplace_back(mapInfo); - std::string key = ""; - auto info = modSourceMap->Find(row, col, targetMap, key); - EXPECT_STREQ(info.sources.c_str(), "file"); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1200 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_1300 - * @tc.name: Find - * @tc.desc: Verify binary search. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_1300, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1300 start"; - auto modSourceMap = std::make_shared(); - int32_t row = 3; - int32_t col = 3; - SourceMapData targetMap; - targetMap.files_.emplace_back("file"); - - for (int32_t i = 0; i < 10; i++) { - for (int32_t j = 0; j < 5; j++) { - SourceMapInfo mapInfo; - mapInfo.beforeRow = 0; - mapInfo.beforeColumn = 0; - mapInfo.afterRow = i; - mapInfo.afterColumn = j; - targetMap.afterPos_.emplace_back(mapInfo); - } - } - - std::string key = ""; - auto info = modSourceMap->Find(row, col, targetMap, key); - EXPECT_EQ(info.row, 1); - EXPECT_EQ(info.col, 1); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1300 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_1400 - * @tc.name: Find - * @tc.desc: Verify binary search. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_1400, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1400 start"; - auto modSourceMap = std::make_shared(); - int32_t row = 7; - int32_t col = 1; - SourceMapData targetMap; - targetMap.files_.emplace_back("file"); - - for (int32_t i = 0; i < 10; i++) { - SourceMapInfo mapInfo; - mapInfo.beforeRow = 0; - mapInfo.beforeColumn = 0; - mapInfo.afterRow = i; - mapInfo.afterColumn = 1; - targetMap.afterPos_.emplace_back(mapInfo); - } - - std::string key = "aaawebpack:///bbb"; - auto info = modSourceMap->Find(row, col, targetMap, key); - EXPECT_EQ(info.row, 1); - EXPECT_EQ(info.col, 1); - EXPECT_STREQ(info.sources.c_str(), "aaabbb"); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1400 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_1500 - * @tc.name: GetPosInfo - * @tc.desc: Verifying GetPosInfo succeeded. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_1500, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1500 start"; - auto modSourceMap = std::make_shared(); - - std::string temp = "TestAbility.js:5779:5"; - int32_t start = 22; - std::string line; - std::string column; - modSourceMap->GetPosInfo(temp, start, line, column); - EXPECT_STREQ(line.c_str(), "5779"); - EXPECT_STREQ(column.c_str(), "5"); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1500 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_1600 - * @tc.name: StringToInt - * @tc.desc: Verifying StringToInt succeeded. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_1600, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1600 start"; - auto modSourceMap = std::make_shared(); - - std::string value = "2030300 This is test"; - auto res = modSourceMap->StringToInt(value); - EXPECT_EQ(res, 2030300); - - value = "2147483648 This is test"; - res = modSourceMap->StringToInt(value); - EXPECT_EQ(res, 0); - - value = ""; - res = modSourceMap->StringToInt(value); - EXPECT_EQ(res, 0); - - value = "-2147483649 This is test"; - res = modSourceMap->StringToInt(value); - EXPECT_EQ(res, 0); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1600 end"; -} - -/** - * @tc.number: AaFwk_SourceMap_1700 - * @tc.name: GetRelativePath - * @tc.desc: Verifying GetRelativePath succeeded. - */ -HWTEST_F(SourceMapTest, AaFwk_SourceMap_1700, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1700 start"; - auto modSourceMap = std::make_shared(); - - std::string sources = "TEST:/data/app/MainAbility.js"; - auto res = modSourceMap->GetRelativePath(sources); - EXPECT_STREQ(res.c_str(), "/data/app/MainAbility.js"); - GTEST_LOG_(INFO) << "AaFwk_SourceMap_1700 end"; -} -} // namespace AppExecFwk -} // namespace OHOS diff --git a/test/unittest/runtime_test/js_runtime_test.cpp b/test/unittest/runtime_test/js_runtime_test.cpp index 21461647ed..876d0416d3 100755 --- a/test/unittest/runtime_test/js_runtime_test.cpp +++ b/test/unittest/runtime_test/js_runtime_test.cpp @@ -376,28 +376,6 @@ HWTEST_F(JsRuntimeTest, JsRuntimeLoadSystemModuleTest_0100, TestSize.Level0) HILOG_INFO("LoadSystemModule end"); } -/** - * @tc.name: JsRuntimeGetSourceMapTest_0100 - * @tc.desc: JsRuntime test for GetSourceMap. - * @tc.type: FUNC - */ -HWTEST_F(JsRuntimeTest, JsRuntimeGetSourceMapTest_0100, TestSize.Level0) -{ - HILOG_INFO("GetSourceMap start"); - - std::unique_ptr jsRuntime = std::make_unique(); - EXPECT_TRUE(jsRuntime != nullptr); - - options_.bundleCodeDir = TEST_CODE_PATH; - options_.isStageModel = true; - jsRuntime->bindSourceMaps_ = std::make_unique(options_.bundleCodeDir, options_.isStageModel); - - auto& sourceMap = jsRuntime->GetSourceMap(); - EXPECT_NE(&sourceMap, nullptr); - - HILOG_INFO("GetSourceMap end"); -} - /** * @tc.name: JsRuntimePostTaskTest_0100 * @tc.desc: JsRuntime test for PostTask.