!3266 Import shared libraries across applications

Merge pull request !3266 from 常佳兴/master
This commit is contained in:
openharmony_ci 2022-12-30 09:19:22 +00:00 committed by Gitee
commit 9d2a4635b2
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
10 changed files with 138 additions and 43 deletions

View File

@ -494,6 +494,26 @@ public:
return assetPath_;
}
void SetBundleName(const CString &bundleName)
{
bundleName_ = bundleName;
}
CString GetBundleName() const
{
return bundleName_;
}
void SetModuleName(const CString &moduleName)
{
moduleName_ = moduleName;
}
CString GetModuleName() const
{
return moduleName_;
}
#if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
CpuProfiler *GetProfiler() const
{
@ -654,7 +674,8 @@ private:
HeapProfilerInterface *heapProfile_ {nullptr};
#endif
CString assetPath_;
CString bundleName_;
CString moduleName_;
// Registered Callbacks
PromiseRejectCallback promiseRejectCallback_ {nullptr};
HostPromiseRejectionTracker hostPromiseRejectionTracker_ {nullptr};

View File

@ -41,7 +41,6 @@
#include "libpandafile/code_data_accessor.h"
#include "libpandafile/file.h"
#include "libpandafile/method_data_accessor-inl.h"
#if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
#include "ecmascript/dfx/cpu_profiler/cpu_profiler.h"
#endif

View File

@ -17,7 +17,6 @@
#include "ecmascript/jspandafile/js_pandafile_manager.h"
#include "ecmascript/jspandafile/program_object.h"
#include "libpandafile/class_data_accessor-inl.h"
namespace panda::ecmascript {
@ -234,32 +233,33 @@ CString JSPandaFile::FindEntryPoint(const CString &recordName) const
return entryPoint;
}
CString JSPandaFile::ParseOhmUrl(const CString &fileName)
CString JSPandaFile::ParseOhmUrl(EcmaVM *vm, const CString &inputFileName, CString &outFileName)
{
CString bundleInstallName(BUNDLE_INSTALL_PATH);
size_t startStrLen = bundleInstallName.length();
size_t pos = CString::npos;
if (fileName.length() > startStrLen && fileName.compare(0, startStrLen, bundleInstallName) == 0) {
if (inputFileName.length() > startStrLen && inputFileName.compare(0, startStrLen, bundleInstallName) == 0) {
pos = startStrLen;
}
CString result = fileName;
CString entryPoint;
if (pos != CString::npos) {
result = fileName.substr(pos);
pos = result.find('/');
if (pos != CString::npos) {
result = result.substr(pos + 1);
pos = inputFileName.find('/', startStrLen);
ASSERT(pos != CString::npos);
CString moduleName = inputFileName.substr(startStrLen, pos - startStrLen);
if (moduleName != vm->GetModuleName()) {
outFileName = CString(BUNDLE_INSTALL_PATH) + moduleName + CString(MERGE_ABC_ETS_MODULES);
}
entryPoint = vm->GetBundleName() + "/" + inputFileName.substr(startStrLen);
} else {
// Temporarily handle the relative path sent by arkui
result = MODULE_DEFAULE_ETS + result;
entryPoint = vm->GetBundleName() + "/" + vm->GetModuleName() + MODULE_DEFAULE_ETS + inputFileName;
}
pos = result.find_last_of(".");
pos = entryPoint.rfind(".abc");
if (pos != CString::npos) {
result = result.substr(0, pos);
entryPoint = entryPoint.substr(0, pos);
}
return result;
return entryPoint;
}
std::string JSPandaFile::ParseHapPath(const CString &fileName)

View File

@ -74,7 +74,7 @@ public:
static constexpr char IS_COMMON_JS[] = "isCommonjs";
static constexpr char MODULE_RECORD_IDX[] = "moduleRecordIdx";
static constexpr char MODULE_DEFAULE_ETS[] = "ets/";
static constexpr char MODULE_DEFAULE_ETS[] = "/ets/";
static constexpr char BUNDLE_INSTALL_PATH[] = "/data/storage/el1/bundle/";
static constexpr char BUNDLE_SUB_INSTALL_PATH[] = "/data/storage/el1/";
static constexpr char NODE_MODULES[] = "node_modules/";
@ -84,7 +84,7 @@ public:
static constexpr char MERGE_ABC_ETS_MODULES[] = "/ets/modules.abc";
static constexpr char PACKAGE_NAME[] = "pkgName@";
static constexpr int PACKAGE_NAME_LEN = 8;
static constexpr int MODULE_PREFIX_LENGTH = 8;
static constexpr int MODULE_OR_BUNDLE_PREFIX_LEN = 8;
static constexpr uint32_t INVALID_INDEX = -1;
JSPandaFile(const panda_file::File *pf, const CString &descriptor);
@ -264,7 +264,7 @@ public:
CString FindEntryPoint(const CString &record) const;
static CString ParseOhmUrl(const CString &fileName);
static CString ParseOhmUrl(EcmaVM *vm, const CString &inputFileName, CString &outFileName);
static std::string ParseHapPath(const CString &fileName);
bool IsSystemLib() const

View File

@ -30,19 +30,22 @@ Expected<JSTaggedValue, bool> JSPandaFileExecutor::ExecuteFromFile(JSThread *thr
LOG_ECMA(DEBUG) << "JSPandaFileExecutor::ExecuteFromFile filename " << filename.c_str();
CString entry;
CString name = filename;
CString name;
CString normalName = NormalizePath(filename);
if (!thread->GetEcmaVM()->IsBundlePack()) {
#if defined(PANDA_TARGET_LINUX) || defined(OHOS_UNIT_TEST)
name = filename;
entry = entryPoint.data();
#else
if (excuteFromJob) {
entry = entryPoint.data();
} else {
entry = JSPandaFile::ParseOhmUrl(normalName);
entry = JSPandaFile::ParseOhmUrl(thread->GetEcmaVM(), normalName, name);
}
#if !WIN_OR_MAC_OR_IOS_PLATFORM
name = thread->GetEcmaVM()->GetAssetPath().c_str();
if (name.empty()) {
name = thread->GetEcmaVM()->GetAssetPath().c_str();
}
#elif defined(PANDA_TARGET_WINDOWS)
CString assetPath = thread->GetEcmaVM()->GetAssetPath().c_str();
name = assetPath + "\\" + JSPandaFile::MERGE_ABC_NAME;
@ -52,6 +55,7 @@ Expected<JSTaggedValue, bool> JSPandaFileExecutor::ExecuteFromFile(JSThread *thr
#endif
#endif
} else {
name = filename;
entry = entryPoint.data();
}
@ -65,7 +69,6 @@ Expected<JSTaggedValue, bool> JSPandaFileExecutor::ExecuteFromFile(JSThread *thr
[[maybe_unused]] EcmaHandleScope scope(thread);
EcmaVM *vm = thread->GetEcmaVM();
ModuleManager *moduleManager = vm->GetModuleManager();
moduleManager->SetExecuteMode(false);
JSHandle<SourceTextModule> moduleRecord(thread->GlobalConstants()->GetHandledUndefined());
if (jsPandaFile->IsBundlePack()) {
moduleRecord = moduleManager->HostResolveImportedModule(name);
@ -121,7 +124,7 @@ Expected<JSTaggedValue, bool> JSPandaFileExecutor::ExecuteModuleBuffer(
name = assetPath + "/" + JSPandaFile::MERGE_ABC_NAME;
#endif
CString normalName = NormalizePath(filename);
CString entry = JSPandaFile::ParseOhmUrl(normalName);
CString entry = JSPandaFile::ParseOhmUrl(thread->GetEcmaVM(), normalName, name);
const JSPandaFile *jsPandaFile =
JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, name, entry.c_str(), buffer, size);
if (jsPandaFile == nullptr) {

View File

@ -25,7 +25,6 @@
#include "ecmascript/js_array.h"
#include "ecmascript/linked_hash_table.h"
#include "ecmascript/module/js_module_source_text.h"
#include "ecmascript/tagged_dictionary.h"
#include "ecmascript/require/js_cjs_module.h"
#ifdef PANDA_TARGET_WINDOWS
@ -289,25 +288,30 @@ JSHandle<SourceTextModule> ModuleManager::HostResolveImportedModuleWithMerge(con
if (entry != -1) {
return JSHandle<SourceTextModule>(thread, dict->GetValue(entry));
}
const JSPandaFile *jsPandaFile =
JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, moduleFileName, recordName);
const JSPandaFile *jsPandaFile = JSPandaFileManager::GetInstance()->FindJSPandaFile(moduleFileName);
if (jsPandaFile == nullptr) {
ResolveBufferCallback resolveBufferCallback = thread->GetEcmaVM()->GetResolveBufferCallback();
if (resolveBufferCallback != nullptr) {
bool mode = GetCurrentMode();
if (mode) {
ResolveBufferCallback resolveBufferCallback = thread->GetEcmaVM()->GetResolveBufferCallback();
if (resolveBufferCallback == nullptr) {
LOG_FULL(FATAL) << "resolveBufferCallback is nullptr";
UNREACHABLE();
}
std::vector<uint8_t> data = resolveBufferCallback(JSPandaFile::ParseHapPath(moduleFileName));
if (data.empty()) {
LOG_FULL(FATAL) << "resolveBufferCallback get buffer failed";
UNREACHABLE();
}
jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, moduleFileName, recordName.c_str(),
data.data(), data.size());
if (jsPandaFile == nullptr) {
LOG_FULL(FATAL) << "open jsPandaFile " << moduleFileName << " error";
UNREACHABLE();
}
data.data(), data.size(), true);
} else {
jsPandaFile = JSPandaFileManager::GetInstance()->LoadJSPandaFile(thread, moduleFileName, recordName, true);
}
}
if (jsPandaFile == nullptr) {
LOG_FULL(FATAL) << "open jsPandaFile " << moduleFileName << " error";
UNREACHABLE();
}
JSHandle<SourceTextModule> moduleRecord = ResolveModuleWithMerge(thread, jsPandaFile, recordName);
JSHandle<NameDictionary> handleDict(thread, resolvedModules_);
@ -609,17 +613,24 @@ CString ModuleManager::ConcatFileNameWithMerge(const JSPandaFile *jsPandaFile, C
size_t pos = 0;
size_t typePos = CString::npos;
if (moduleRequestName.find("@bundle:") != CString::npos) {
moduleRequestName = moduleRequestName.substr(JSPandaFile::MODULE_OR_BUNDLE_PREFIX_LEN);
pos = moduleRequestName.find('/');
pos = moduleRequestName.find('/', pos + 1);
ASSERT(pos != CString::npos);
entryPoint = moduleRequestName.substr(pos + 1);
CString bundleName = moduleRequestName.substr(0, pos);
size_t bundleNameLen = bundleName.length();
if (moduleRecordName.length() > bundleNameLen && moduleRecordName.compare(0, bundleNameLen, bundleName) != 0) {
pos = moduleRequestName.find('/', bundleNameLen + 1);
baseFilename = JSPandaFile::BUNDLE_INSTALL_PATH + moduleRequestName.substr(0, pos) +
JSPandaFile::MERGE_ABC_ETS_MODULES;
}
entryPoint = moduleRequestName;
} else if (moduleRequestName.find("@module:") != CString::npos) {
moduleRequestName = moduleRequestName.substr(JSPandaFile::MODULE_PREFIX_LENGTH);
moduleRequestName = moduleRequestName.substr(JSPandaFile::MODULE_OR_BUNDLE_PREFIX_LEN);
pos = moduleRequestName.find('/');
ASSERT(pos != CString::npos);
baseFilename =
JSPandaFile::BUNDLE_INSTALL_PATH + moduleRequestName.substr(0, pos) + JSPandaFile::MERGE_ABC_ETS_MODULES;
entryPoint = moduleRequestName.substr(pos + 1);
pos = moduleRecordName.find('/');
entryPoint = moduleRecordName.substr(0, pos + 1) + moduleRequestName;
} else if (IsImportedPath(moduleRequestName, typePos)) {
if (typePos != CString::npos) {
moduleRequestName = moduleRequestName.substr(0, typePos);
@ -638,7 +649,6 @@ CString ModuleManager::ConcatFileNameWithMerge(const JSPandaFile *jsPandaFile, C
if (!jsPandaFile->HasRecord(entryPoint)) {
entryPoint += "/index";
}
if (!jsPandaFile->HasRecord(entryPoint)) {
pos = baseFilename.rfind('/');
if (pos != CString::npos) {

View File

@ -100,7 +100,7 @@ private:
EcmaVM *vm_ {nullptr};
JSTaggedValue resolvedModules_ {JSTaggedValue::Hole()};
bool isExecuteBuffer_ = false;
bool isExecuteBuffer_ {false};
friend class EcmaVM;
friend class PatchLoader;

View File

@ -217,9 +217,15 @@ HWTEST_F_L0(EcmaModuleTest, ConcatFileNameWithMerge1)
// Test moduleRequestName start with "@bundle"
CString moduleRecordName = "moduleTest1";
CString moduleRequestName = "@bundle:com.bundleName.test/moduleName/requestModuleName1";
CString result = "requestModuleName1";
CString result = "com.bundleName.test/moduleName/requestModuleName1";
CString entryPoint = ModuleManager::ConcatFileNameWithMerge(pf, baseFilename, moduleRecordName, moduleRequestName);
EXPECT_EQ(result, entryPoint);
// Test cross application
moduleRecordName = "@bundle:com.bundleName1.test/moduleName/requestModuleName1";
CString newBaseFileName = "/data/storage/el1/bundle/com.bundleName.test/moduleName/ets/modules.abc";
ModuleManager::ConcatFileNameWithMerge(pf, baseFilename, moduleRecordName, moduleRequestName);
EXPECT_EQ(baseFilename, newBaseFileName);
}
HWTEST_F_L0(EcmaModuleTest, ConcatFileNameWithMerge2)
@ -293,6 +299,7 @@ HWTEST_F_L0(EcmaModuleTest, ConcatFileNameWithMerge3)
entryPoint = ModuleManager::ConcatFileNameWithMerge(pf2, baseFilename, moduleRecordName, moduleRequestName);
EXPECT_EQ(baseFilename, requestFileName);
EXPECT_EQ(result, entryPoint);
}
HWTEST_F_L0(EcmaModuleTest, ConcatFileNameWithMerge4)
@ -323,6 +330,32 @@ HWTEST_F_L0(EcmaModuleTest, ConcatFileNameWithMerge4)
EXPECT_EQ(result, entryPoint);
}
HWTEST_F_L0(EcmaModuleTest, ConcatFileNameWithMerge5)
{
CString baseFilename = "merge.abc";
const char *data = R"(
.language ECMAScript
.function any func_main_0(any a0, any a1, any a2) {
ldai 1
return
}
)";
JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
Parser parser;
auto res = parser.Parse(data);
std::unique_ptr<const File> pfPtr = pandasm::AsmEmitter::Emit(res.Value());
JSPandaFile *pf = pfManager->NewJSPandaFile(pfPtr.release(), baseFilename);
// Test moduleRequestName start with "@module"
CString moduleRecordName = "com.bundleName.test/moduleName1/moduleTest1";
CString moduleRequestName = "@module:moduleName/requestModuleName1";
CString result = "com.bundleName.test/moduleName/requestModuleName1";
CString newBaseFileName = "/data/storage/el1/bundle/moduleName/ets/modules.abc";
CString entryPoint = ModuleManager::ConcatFileNameWithMerge(pf, baseFilename, moduleRecordName, moduleRequestName);
EXPECT_EQ(result, entryPoint);
EXPECT_EQ(baseFilename, newBaseFileName);
}
HWTEST_F_L0(EcmaModuleTest, GetRecordName1)
{
std::string baseFileName = MODULE_ABC_PATH "module_test_module_test_module_base.abc";

View File

@ -1294,6 +1294,11 @@ public:
static void SetAssetPath(EcmaVM *vm, const std::string &assetPath);
static void SetLoop(EcmaVM *vm, void *loop);
static std::string GetAssetPath(EcmaVM *vm);
static void SetBundleName(EcmaVM *vm, std::string bundleName);
static std::string GetBundleName(EcmaVM *vm);
static void SetModuleName(EcmaVM *vm, std::string moduleName);
static std::string GetModuleName(EcmaVM *vm);
//static std::string SetCurrentBundleName(EcmaVM *vm);
private:
static int vmCount_;

View File

@ -645,7 +645,8 @@ Local<ObjectRef> JSNApi::GetExportObject(EcmaVM *vm, const std::string &file, co
{
ecmascript::CString entry = file.c_str();
if (!vm->IsBundlePack()) {
entry = ecmascript::JSPandaFile::ParseOhmUrl(entry);
ecmascript::CString name;
entry = ecmascript::JSPandaFile::ParseOhmUrl(vm, entry, name);
}
ecmascript::ModuleManager *moduleManager = vm->GetModuleManager();
JSThread *thread = vm->GetJSThread();
@ -2677,4 +2678,27 @@ std::string JSNApi::GetAssetPath(EcmaVM *vm)
{
return vm->GetAssetPath().c_str();
}
void JSNApi::SetBundleName(EcmaVM *vm, std::string bundleName)
{
ecmascript::CString name = bundleName.c_str();
vm->SetBundleName(name);
}
std::string JSNApi::GetBundleName(EcmaVM *vm)
{
return vm->GetBundleName().c_str();
}
void JSNApi::SetModuleName(EcmaVM *vm, std::string moduleName)
{
ecmascript::CString name = moduleName.c_str();
vm->SetModuleName(name);
}
std::string JSNApi::GetModuleName(EcmaVM *vm)
{
return vm->GetModuleName().c_str();
}
} // namespace panda