!5347 FA模型适配sourceMap

Merge pull request !5347 from FangJinliang/pr_0504
This commit is contained in:
openharmony_ci 2023-05-08 07:18:23 +00:00 committed by Gitee
commit e2cc37a935
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
11 changed files with 79 additions and 126 deletions

View File

@ -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
@ -435,6 +434,7 @@ bool JsRuntime::Initialize(const Options& options)
}
}
bool isModular = false;
if (IsUseAbilityRuntime(options)) {
HandleScope handleScope(*this);
auto nativeEngine = GetNativeEnginePointer();
@ -504,15 +504,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<JsEnv::SourceMap>();
bool isModular = !panda::JSNApi::IsBundle(vm);
auto operatorImpl = std::make_shared<JsSourceMapOperatorImpl>(options.hapPath, isModular, bindSourceMaps);
InitSourceMap(operatorImpl);
if (options.isUnique) {
HILOG_INFO("Not supported TimerModule when form render");
@ -525,6 +522,9 @@ bool JsRuntime::Initialize(const Options& options)
}
}
auto operatorObj = std::make_shared<JsEnv::SourceMapOperator>(options.hapPath, isModular);
InitSourceMap(operatorObj);
preloaded_ = options.preload;
return true;
}
@ -629,10 +629,10 @@ void JsRuntime::SetAppLibPath(const AppLibPathMap& appLibPaths, const bool& isSy
}
}
void JsRuntime::InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperatorImpl> operatorImpl)
void JsRuntime::InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorObj)
{
CHECK_POINTER(jsEnv_);
jsEnv_->InitSourceMap(operatorImpl);
jsEnv_->InitSourceMap(operatorObj);
JsEnv::SourceMap::RegisterReadSourceMapCallback(JsRuntime::ReadSourceMapData);
}
@ -987,8 +987,9 @@ void JsRuntime::RegisterQuickFixQueryFunc(const std::map<std::string, std::strin
}
}
bool JsRuntime::ReadSourceMapData(const std::string& hapPath, std::string& content)
bool JsRuntime::ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content)
{
// Source map relative path, FA: "/assets/js", Stage: "/ets"
if (hapPath.empty()) {
HILOG_ERROR("hapPath is empty");
return false;
@ -1002,9 +1003,13 @@ bool JsRuntime::ReadSourceMapData(const std::string& hapPath, std::string& conte
}
std::unique_ptr<uint8_t[]> 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<char*>(dataPtr.get());
return true;

View File

@ -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

View File

@ -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<JsEnv::SourceMap> 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<JsEnv::SourceMap> bindSourceMaps_ = nullptr;
};
} // namespace AbilityRuntime
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_JS_SOURCE_MAP_OPERATOR_H

View File

@ -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",

View File

@ -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;
@ -110,7 +110,7 @@ public:
void RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath) override;
static bool GetFileBuffer(const std::string& filePath, std::string& fileFullName, std::vector<uint8_t>& buffer);
void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperatorImpl> operatorImpl);
void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorImpl);
void FreeNativeReference(std::unique_ptr<NativeReference> reference);
void FreeNativeReference(std::shared_ptr<NativeReference>&& reference);

View File

@ -98,9 +98,9 @@ void JsEnvironment::RemoveTask(const std::string& name)
}
}
void JsEnvironment::InitSourceMap(const std::shared_ptr<SourceMapOperatorImpl> operatorImpl)
void JsEnvironment::InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorObj)
{
sourceMapOperator_ = std::make_shared<SourceMapOperator>(operatorImpl);
sourceMapOperator_ = operatorObj;
}
void JsEnvironment::RegisterUncaughtExceptionHandler(JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo)

View File

@ -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<SourceMapData>();
}
}
}
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;
}

View File

@ -50,7 +50,7 @@ public:
void InitWorkerModule(const std::string& codePath, bool isDebugVersion, bool isBundle);
void InitSourceMap(const std::shared_ptr<SourceMapOperatorImpl> operatorImpl);
void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorObj);
void InitSyscapModule();

View File

@ -60,21 +60,22 @@ public:
}
};
using ReadSourceMapCallback = std::function<bool(const std::string& hapPath, std::string& content)>;
using ReadSourceMapCallback = std::function<bool(const std::string& hapPath,
const std::string& sourceMapPath, std::string& content)>;
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<SourceMapData> 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<std::string, std::shared_ptr<SourceMapData>> sourceMaps_;
std::shared_ptr<SourceMapData> nonModularMap_;
static ReadSourceMapCallback readSourceMapFunc_;

View File

@ -18,32 +18,28 @@
#include <memory>
#include <string>
#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<SourceMapOperatorImpl> 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<SourceMapOperatorImpl> impl_ = nullptr;
const std::string hapPath_;
bool isModular_ = false;
};
} // namespace JsEnv
} // namespace OHOS

View File

@ -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<SourceMap>();
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<SourceMap>();
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<SourceMap>();
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");