From dbaac31521cc1a9f7e0dc7165b7f338288b5d7a4 Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Thu, 4 May 2023 10:15:15 +0800 Subject: [PATCH] support pa engine sourceMap Signed-off-by: fangJinliang1 Change-Id: Ic152b4e918feadddd7cfaa9ae0cb2c085fdee300 Signed-off-by: fangJinliang1 --- frameworks/native/runtime/js_runtime.cpp | 29 +++++++------ .../native/runtime/js_source_map_operator.cpp | 34 --------------- .../native/runtime/js_source_map_operator.h | 42 ------------------- interfaces/inner_api/runtime/BUILD.gn | 1 - .../inner_api/runtime/include/js_runtime.h | 6 +-- .../js_environment/src/js_environment.cpp | 4 +- .../js_environment/src/source_map.cpp | 38 +++++++++++++---- .../interfaces/inner_api/js_environment.h | 2 +- .../interfaces/inner_api/source_map.h | 8 ++-- .../inner_api/source_map_operator.h | 24 +++++------ .../source_map_test/source_map_test.cpp | 17 ++++---- 11 files changed, 79 insertions(+), 126 deletions(-) delete mode 100644 frameworks/native/runtime/js_source_map_operator.cpp delete mode 100644 frameworks/native/runtime/js_source_map_operator.h diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 0d03a11fda..6125342306 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -41,7 +41,6 @@ #include "js_module_searcher.h" #include "js_quickfix_callback.h" #include "js_runtime_utils.h" -#include "js_source_map_operator.h" #include "js_timer.h" #include "js_utils.h" #include "js_worker.h" @@ -52,6 +51,7 @@ #include "extractor.h" #include "systemcapability.h" #include "source_map.h" +#include "source_map_operator.h" #ifdef SUPPORT_GRAPHICS #include "declarative_module_preloader.h" @@ -67,7 +67,6 @@ 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 @@ -438,6 +437,7 @@ bool JsRuntime::Initialize(const Options& options) } } + bool isModular = false; if (IsUseAbilityRuntime(options)) { HandleScope handleScope(*this); auto nativeEngine = GetNativeEnginePointer(); @@ -507,15 +507,12 @@ bool JsRuntime::Initialize(const Options& options) panda::JSNApi::SetBundle(vm, options.isBundle); panda::JSNApi::SetBundleName(vm, options.bundleName); panda::JSNApi::SetHostResolveBufferTracker(vm, JsModuleReader(options.bundleName)); + isModular = !panda::JSNApi::IsBundle(vm); if (!InitLoop(options.eventRunner)) { HILOG_ERROR("Initialize loop failed."); return false; } - auto bindSourceMaps = std::make_shared(); - bool isModular = !panda::JSNApi::IsBundle(vm); - auto operatorImpl = std::make_shared(options.hapPath, isModular, bindSourceMaps); - InitSourceMap(operatorImpl); if (options.isUnique) { HILOG_INFO("Not supported TimerModule when form render"); @@ -528,6 +525,9 @@ bool JsRuntime::Initialize(const Options& options) } } + auto operatorObj = std::make_shared(options.hapPath, isModular); + InitSourceMap(operatorObj); + preloaded_ = options.preload; return true; } @@ -632,10 +632,10 @@ void JsRuntime::SetAppLibPath(const AppLibPathMap& appLibPaths, const bool& isSy } } -void JsRuntime::InitSourceMap(const std::shared_ptr operatorImpl) +void JsRuntime::InitSourceMap(const std::shared_ptr operatorObj) { CHECK_POINTER(jsEnv_); - jsEnv_->InitSourceMap(operatorImpl); + jsEnv_->InitSourceMap(operatorObj); JsEnv::SourceMap::RegisterReadSourceMapCallback(JsRuntime::ReadSourceMapData); } @@ -990,8 +990,9 @@ void JsRuntime::RegisterQuickFixQueryFunc(const std::map dataPtr = nullptr; size_t len = 0; - if (!extractor->ExtractToBufByName(MEGER_SOURCE_MAP_PATH, dataPtr, len)) { - HILOG_ERROR("get mergeSourceMapData fileBuffer failed"); - return false; + if (!extractor->ExtractToBufByName(sourceMapPath, dataPtr, len)) { + HILOG_DEBUG("can't find source map, and switch to stage model."); + std::string tempPath = std::regex_replace(sourceMapPath, std::regex("ets"), "assets/js"); + if (!extractor->ExtractToBufByName(tempPath, dataPtr, len)) { + HILOG_ERROR("get mergeSourceMapData fileBuffer failed, map path: %{private}s", tempPath.c_str()); + return false; + } } content = reinterpret_cast(dataPtr.get()); return true; diff --git a/frameworks/native/runtime/js_source_map_operator.cpp b/frameworks/native/runtime/js_source_map_operator.cpp deleted file mode 100644 index e908c7f454..0000000000 --- a/frameworks/native/runtime/js_source_map_operator.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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. - */ - -#include "js_source_map_operator.h" -#include "hilog_wrapper.h" - -namespace OHOS { -namespace AbilityRuntime { -std::string JsSourceMapOperatorImpl::TranslateBySourceMap(const std::string& stackStr) -{ - if (bindSourceMaps_ == nullptr) { - HILOG_ERROR("Source map is invalid."); - return ""; - } - - std::string sourceMapData; - JsEnv::SourceMap::ReadSourceMapData(hapPath_, sourceMapData); - bindSourceMaps_->Init(isModular_, sourceMapData); - return bindSourceMaps_->TranslateBySourceMap(stackStr); -} -} // namespace AbilityRuntime -} // namespace OHOS diff --git a/frameworks/native/runtime/js_source_map_operator.h b/frameworks/native/runtime/js_source_map_operator.h deleted file mode 100644 index 7cb29a1730..0000000000 --- a/frameworks/native/runtime/js_source_map_operator.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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_RUNTIME_JS_SOURCE_MAP_OPERATOR_H -#define OHOS_ABILITY_RUNTIME_JS_SOURCE_MAP_OPERATOR_H - -#include "source_map.h" -#include "source_map_operator.h" - -namespace OHOS { -namespace AbilityRuntime { -class JsSourceMapOperatorImpl : public JsEnv::SourceMapOperatorImpl { -public: - JsSourceMapOperatorImpl(const std::string hapPath, bool isModular, std::shared_ptr bindSourceMaps) - : hapPath_(hapPath), isModular_(isModular), bindSourceMaps_(bindSourceMaps) - {} - - ~JsSourceMapOperatorImpl() = default; - -std::string TranslateBySourceMap(const std::string& stackStr) override; - -private: - const std::string hapPath_; - bool isModular_ = false; - std::shared_ptr bindSourceMaps_ = nullptr; -}; -} // namespace AbilityRuntime -} // namespace OHOS - -#endif // OHOS_ABILITY_RUNTIME_JS_SOURCE_MAP_OPERATOR_H diff --git a/interfaces/inner_api/runtime/BUILD.gn b/interfaces/inner_api/runtime/BUILD.gn index 61628157fb..685c19e738 100644 --- a/interfaces/inner_api/runtime/BUILD.gn +++ b/interfaces/inner_api/runtime/BUILD.gn @@ -58,7 +58,6 @@ ohos_shared_library("runtime") { "${ability_runtime_native_path}/runtime/js_quickfix_callback.cpp", "${ability_runtime_native_path}/runtime/js_runtime.cpp", "${ability_runtime_native_path}/runtime/js_runtime_utils.cpp", - "${ability_runtime_native_path}/runtime/js_source_map_operator.cpp", "${ability_runtime_native_path}/runtime/js_timer.cpp", "${ability_runtime_native_path}/runtime/js_worker.cpp", "${ability_runtime_native_path}/runtime/ohos_js_env_logger.cpp", diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index 4e81d89666..32f8c7ab8c 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -40,7 +40,7 @@ class Extractor; namespace JsEnv { class JsEnvironment; -class SourceMapOperatorImpl; +class SourceMapOperator; struct UncaughtExceptionInfo; } // namespace JsEnv @@ -63,7 +63,7 @@ public: static void SetAppLibPath(const AppLibPathMap& appLibPaths, const bool& isSystemApp = false); - static bool ReadSourceMapData(const std::string& hapPath, std::string& content); + static bool ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content); JsRuntime(); ~JsRuntime() override; @@ -107,7 +107,7 @@ public: void RegisterQuickFixQueryFunc(const std::map& moduleAndPath) override; static bool GetFileBuffer(const std::string& filePath, std::string& fileFullName, std::vector& buffer); - void InitSourceMap(const std::shared_ptr operatorImpl); + void InitSourceMap(const std::shared_ptr operatorImpl); void FreeNativeReference(std::unique_ptr reference); void FreeNativeReference(std::shared_ptr&& reference); diff --git a/js_environment/frameworks/js_environment/src/js_environment.cpp b/js_environment/frameworks/js_environment/src/js_environment.cpp index 3c80f8e688..c6efdf1a54 100644 --- a/js_environment/frameworks/js_environment/src/js_environment.cpp +++ b/js_environment/frameworks/js_environment/src/js_environment.cpp @@ -106,9 +106,9 @@ void JsEnvironment::RemoveTask(const std::string& name) } } -void JsEnvironment::InitSourceMap(const std::shared_ptr operatorImpl) +void JsEnvironment::InitSourceMap(const std::shared_ptr operatorObj) { - sourceMapOperator_ = std::make_shared(operatorImpl); + sourceMapOperator_ = operatorObj; } void JsEnvironment::RegisterUncaughtExceptionHandler(JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo) diff --git a/js_environment/frameworks/js_environment/src/source_map.cpp b/js_environment/frameworks/js_environment/src/source_map.cpp index 3c8ecc9a43..319d6e3e03 100644 --- a/js_environment/frameworks/js_environment/src/source_map.cpp +++ b/js_environment/frameworks/js_environment/src/source_map.cpp @@ -46,6 +46,7 @@ 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"; +const std::string MEGER_SOURCE_MAP_PATH = "ets/sourceMaps.map"; } // namespace ReadSourceMapCallback SourceMap::readSourceMapFunc_ = nullptr; @@ -82,10 +83,19 @@ uint32_t Base64CharToInt(char charCode) return DIGIT_NUM; }; -void SourceMap::Init(bool isModular, const std::string& sourceMap) +void SourceMap::Init(bool isModular, const std::string& hapPath) { isModular_ = isModular; - SplitSourceMap(sourceMap); + hapPath_ = hapPath; + if (isModular_) { + std::string sourceMapData; + ReadSourceMapData(hapPath_, MEGER_SOURCE_MAP_PATH, sourceMapData); + SplitSourceMap(sourceMapData); + } else { + if (!nonModularMap_) { + nonModularMap_ = std::make_shared(); + } + } } std::string SourceMap::TranslateBySourceMap(const std::string& stackStr) @@ -116,8 +126,15 @@ std::string SourceMap::TranslateBySourceMap(const std::string& stackStr) // 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(":"); + size_t start; + size_t end; + if (isModular_) { + start = temp.find(openBrace); + end = temp.find(":"); + } else { + start = temp.find("/ets/"); + end = temp.rfind("_.js"); + } if (end <= start) { continue; } @@ -138,7 +155,14 @@ std::string SourceMap::TranslateBySourceMap(const std::string& stackStr) sourceInfo = GetSourceInfo(line, column, *(iter->second), key); } } else { - sourceInfo = GetSourceInfo(line, column, *nonModularMap_, key); + std::string url = key + ".js.map"; + std::string curSourceMap; + if (!ReadSourceMapData(hapPath_, url, curSourceMap)) { + JSENV_LOG_W("ReadSourceMapData fail"); + continue; + } + ExtractSourceMapData(curSourceMap, nonModularMap_); + sourceInfo = GetSourceInfo(line, column, *nonModularMap_, key + ".ts"); } if (sourceInfo.empty()) { break; @@ -542,10 +566,10 @@ void SourceMap::RegisterReadSourceMapCallback(ReadSourceMapCallback readFunc) readSourceMapFunc_ = readFunc; } -bool SourceMap::ReadSourceMapData(const std::string& hapPath, std::string& content) +bool SourceMap::ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content) { if (readSourceMapFunc_) { - return readSourceMapFunc_(hapPath, content); + return readSourceMapFunc_(hapPath, sourceMapPath, content); } return false; } diff --git a/js_environment/interfaces/inner_api/js_environment.h b/js_environment/interfaces/inner_api/js_environment.h index 78b97eb743..17afbc225d 100644 --- a/js_environment/interfaces/inner_api/js_environment.h +++ b/js_environment/interfaces/inner_api/js_environment.h @@ -54,7 +54,7 @@ public: void InitWorkerModule(const std::string& codePath, bool isDebugVersion, bool isBundle); - void InitSourceMap(const std::shared_ptr operatorImpl); + void InitSourceMap(const std::shared_ptr operatorObj); void InitSyscapModule(); diff --git a/js_environment/interfaces/inner_api/source_map.h b/js_environment/interfaces/inner_api/source_map.h index 556433b01a..7ef19b85c4 100644 --- a/js_environment/interfaces/inner_api/source_map.h +++ b/js_environment/interfaces/inner_api/source_map.h @@ -60,21 +60,22 @@ public: } }; -using ReadSourceMapCallback = std::function; +using ReadSourceMapCallback = std::function; class SourceMap final { public: SourceMap() = default; ~SourceMap() = default; - void Init(bool isModular, const std::string& sourceMap); + void Init(bool isModular, const std::string& hapPath); 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); + static bool ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content); private: void SplitSourceMap(const std::string& sourceMapData); @@ -91,6 +92,7 @@ private: private: bool isModular_ = true; + std::string hapPath_; std::unordered_map> sourceMaps_; std::shared_ptr nonModularMap_; static ReadSourceMapCallback readSourceMapFunc_; diff --git a/js_environment/interfaces/inner_api/source_map_operator.h b/js_environment/interfaces/inner_api/source_map_operator.h index cab40e1b23..1e110df8e4 100644 --- a/js_environment/interfaces/inner_api/source_map_operator.h +++ b/js_environment/interfaces/inner_api/source_map_operator.h @@ -18,32 +18,28 @@ #include #include +#include "js_env_logger.h" +#include "source_map.h" + namespace OHOS { namespace JsEnv { -class SourceMapOperatorImpl { -public: - SourceMapOperatorImpl() = default; - virtual ~SourceMapOperatorImpl() = default; - virtual std::string TranslateBySourceMap(const std::string& stackStr) = 0; -}; - class SourceMapOperator { public: - SourceMapOperator(std::shared_ptr impl) : impl_(impl) - {} + SourceMapOperator(const std::string hapPath, bool isModular) + : hapPath_(hapPath), isModular_(isModular) {} ~SourceMapOperator() = default; std::string TranslateBySourceMap(const std::string& stackStr) { - if (impl_ == nullptr) { - return ""; - } - return impl_->TranslateBySourceMap(stackStr); + SourceMap sourceMapObj; + sourceMapObj.Init(isModular_, hapPath_); + return sourceMapObj.TranslateBySourceMap(stackStr); } private: - std::shared_ptr impl_ = nullptr; + const std::string hapPath_; + bool isModular_ = false; }; } // namespace JsEnv } // namespace OHOS 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 index 85bec5de81..f05a45630d 100644 --- a/js_environment/test/unittest/source_map_test/source_map_test.cpp +++ b/js_environment/test/unittest/source_map_test/source_map_test.cpp @@ -49,9 +49,9 @@ void SourceMapTest::TearDown(void) { } -bool ReadSourceMapData(const std::string& hapPath, std::string& content) +bool ReadSourceMapData(const std::string& hapPath, const std::string& mapPath, std::string& content) { - if (hapPath.empty()) { + if (hapPath.empty() || mapPath.empty()) { return false; } content = "abc"; @@ -155,9 +155,10 @@ 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 hapPath = "/data/app/test.hap"; + std::string mapPath = "./abc.map"; std::string context; - EXPECT_FALSE(modSourceMap->ReadSourceMapData(filePath, context)); + EXPECT_FALSE(modSourceMap->ReadSourceMapData(hapPath, mapPath, context)); GTEST_LOG_(INFO) << "JsEnv_SourceMap_0600 end"; } @@ -172,9 +173,10 @@ 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 hapPath = "/data/app/test.hap"; + std::string mapPath = "./abc.map"; std::string context; - modSourceMap->ReadSourceMapData(filePath, context); + modSourceMap->ReadSourceMapData(hapPath, mapPath, context); EXPECT_STREQ(context.c_str(), "abc"); GTEST_LOG_(INFO) << "JsEnv_SourceMap_0700 end"; } @@ -385,7 +387,8 @@ HWTEST_F(SourceMapTest, JsEnv_SourceMap_1500, Function | MediumTest | Level1) std::string stackStr = "at anonymous (entry/src/main/ets/pages/Index.ets:111:13)"; auto mapObj = std::make_shared(); - mapObj->Init(true, sourceMaps); + mapObj->Init(true, ""); + mapObj->SplitSourceMap(sourceMaps); std::string stack = mapObj->TranslateBySourceMap(stackStr); EXPECT_STREQ(stack.c_str(), "at anonymous (/ets/pages/Index.ets:85:9)\n");