2022-02-06 15:16:08 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-28 07:04:37 +00:00
|
|
|
#ifndef ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_H
|
|
|
|
#define ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_H
|
2022-02-06 15:16:08 +00:00
|
|
|
|
2022-04-16 09:32:42 +00:00
|
|
|
#include "ecmascript/common.h"
|
2022-06-16 09:27:35 +00:00
|
|
|
#include "ecmascript/js_function.h"
|
2022-02-28 07:04:37 +00:00
|
|
|
#include "ecmascript/jspandafile/constpool_value.h"
|
2022-08-16 13:05:02 +00:00
|
|
|
#include "ecmascript/jspandafile/method_literal.h"
|
2022-03-10 09:02:04 +00:00
|
|
|
#include "ecmascript/mem/c_containers.h"
|
2022-07-23 10:33:33 +00:00
|
|
|
|
2022-02-06 15:16:08 +00:00
|
|
|
#include "libpandafile/file.h"
|
|
|
|
namespace panda {
|
|
|
|
namespace ecmascript {
|
|
|
|
class JSPandaFile {
|
|
|
|
public:
|
2022-08-30 12:52:16 +00:00
|
|
|
struct JSRecordInfo {
|
|
|
|
uint32_t mainMethodIndex {0};
|
|
|
|
bool isCjs {false};
|
2022-09-17 06:19:16 +00:00
|
|
|
bool hasParsedLiteralConstPool {false};
|
2022-08-30 12:52:16 +00:00
|
|
|
int moduleRecordIdx {-1};
|
2022-09-06 02:19:13 +00:00
|
|
|
CUnorderedMap<uint32_t, uint64_t> constpoolMap;
|
2022-10-20 03:08:39 +00:00
|
|
|
bool hasTSTypes {false};
|
|
|
|
uint32_t typeSummaryOffset {0};
|
|
|
|
|
|
|
|
bool HasTSTypes () const
|
|
|
|
{
|
|
|
|
return hasTSTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetTypeSummaryOffset() const
|
|
|
|
{
|
|
|
|
return typeSummaryOffset;
|
|
|
|
}
|
2022-08-30 12:52:16 +00:00
|
|
|
};
|
2022-04-18 04:39:12 +00:00
|
|
|
static constexpr char ENTRY_FUNCTION_NAME[] = "func_main_0";
|
|
|
|
static constexpr char ENTRY_MAIN_FUNCTION[] = "_GLOBAL::func_main_0";
|
2022-08-27 03:52:15 +00:00
|
|
|
static constexpr char PATCH_ENTRY_FUNCTION[] = "_GLOBAL::patch_main_0";
|
2022-09-09 03:49:20 +00:00
|
|
|
static constexpr char PATCH_FUNCTION_NAME_0[] = "patch_main_0";
|
|
|
|
static constexpr char PATCH_FUNCTION_NAME_1[] = "patch_main_1";
|
2022-08-27 03:52:15 +00:00
|
|
|
|
2022-04-18 04:39:12 +00:00
|
|
|
static constexpr char MODULE_CLASS[] = "L_ESModuleRecord;";
|
2022-06-08 02:59:06 +00:00
|
|
|
static constexpr char COMMONJS_CLASS[] = "L_CommonJsRecord;";
|
2022-04-18 04:39:12 +00:00
|
|
|
static constexpr char TYPE_FLAG[] = "typeFlag";
|
2022-09-30 02:00:06 +00:00
|
|
|
static constexpr char TYPE_SUMMARY_OFFSET[] = "typeSummaryOffset";
|
2022-08-31 10:21:38 +00:00
|
|
|
|
2022-08-30 12:52:16 +00:00
|
|
|
static constexpr char IS_COMMON_JS[] = "isCommonjs";
|
|
|
|
static constexpr char MODULE_RECORD_IDX[] = "moduleRecordIdx";
|
|
|
|
static constexpr char MODULE_DEFAULE_ETS[] = "ets/";
|
|
|
|
static constexpr char BUNDLE_INSTALL_PATH[] = "/data/storage/el1/bundle/";
|
|
|
|
static constexpr char NODE_MODULES[] = "node_modules";
|
|
|
|
static constexpr char NODE_MODULES_ZERO[] = "node_modules/0/";
|
|
|
|
static constexpr char NODE_MODULES_ONE[] = "node_modules/1/";
|
2022-10-30 08:19:34 +00:00
|
|
|
static constexpr char MERGE_ABC_NAME[] = "modules.abc";
|
2022-04-18 04:39:12 +00:00
|
|
|
|
2022-10-09 10:01:37 +00:00
|
|
|
JSPandaFile(const panda_file::File *pf, const CString &descriptor);
|
2022-02-06 15:16:08 +00:00
|
|
|
~JSPandaFile();
|
|
|
|
|
2022-03-31 15:38:07 +00:00
|
|
|
const CString &GetJSPandaFileDesc() const
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
|
|
|
return desc_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const panda_file::File *GetPandaFile() const
|
|
|
|
{
|
|
|
|
return pf_;
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:53:58 +00:00
|
|
|
std::string GetFileName() const
|
|
|
|
{
|
|
|
|
return pf_->GetFilename();
|
|
|
|
}
|
|
|
|
|
2022-08-23 01:56:32 +00:00
|
|
|
MethodLiteral* GetMethodLiterals() const
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
2022-08-23 01:56:32 +00:00
|
|
|
return methodLiterals_;
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 03:52:15 +00:00
|
|
|
void SetMethodLiteralToMap(MethodLiteral *methodLiteral)
|
2022-03-10 09:02:04 +00:00
|
|
|
{
|
2022-08-23 01:56:32 +00:00
|
|
|
if (methodLiteral != nullptr) {
|
2022-08-27 03:52:15 +00:00
|
|
|
methodLiteralMap_.emplace(methodLiteral->GetMethodId().GetOffset(), methodLiteral);
|
2022-03-10 09:02:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 03:52:15 +00:00
|
|
|
const CUnorderedMap<uint32_t, MethodLiteral *> &GetMethodLiteralMap() const
|
|
|
|
{
|
|
|
|
return methodLiteralMap_;
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:16:08 +00:00
|
|
|
uint32_t GetNumMethods() const
|
|
|
|
{
|
|
|
|
return numMethods_;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetConstpoolIndex() const
|
|
|
|
{
|
|
|
|
return constpoolIndex_;
|
|
|
|
}
|
|
|
|
|
2022-08-30 12:52:16 +00:00
|
|
|
uint32_t GetMainMethodIndex(const CString &recordName = ENTRY_FUNCTION_NAME) const
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
2022-09-09 08:44:33 +00:00
|
|
|
if (IsBundlePack()) {
|
|
|
|
return jsRecordInfo_.begin()->second.mainMethodIndex;
|
|
|
|
}
|
2022-08-30 12:52:16 +00:00
|
|
|
auto info = jsRecordInfo_.find(recordName);
|
|
|
|
if (info != jsRecordInfo_.end()) {
|
|
|
|
return info->second.mainMethodIndex;
|
|
|
|
}
|
|
|
|
return 0;
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 04:04:15 +00:00
|
|
|
const CUnorderedMap<uint32_t, uint64_t> *GetConstpoolMapByReocrd(const CString &recordName) const
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
2022-09-06 02:19:13 +00:00
|
|
|
auto info = jsRecordInfo_.find(recordName);
|
|
|
|
if (info != jsRecordInfo_.end()) {
|
2022-09-22 04:04:15 +00:00
|
|
|
return &info->second.constpoolMap;
|
2022-09-06 02:19:13 +00:00
|
|
|
}
|
2022-10-12 09:22:34 +00:00
|
|
|
LOG_FULL(FATAL) << "find entryPoint failed: " << recordName;
|
2022-09-06 14:50:58 +00:00
|
|
|
UNREACHABLE();
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 04:04:15 +00:00
|
|
|
const CUnorderedMap<uint32_t, uint64_t> &GetConstpoolMap() const
|
|
|
|
{
|
|
|
|
return constpoolMap_;
|
|
|
|
}
|
|
|
|
|
2022-09-06 02:19:13 +00:00
|
|
|
uint32_t PUBLIC_API GetOrInsertConstantPool(ConstPoolType type, uint32_t offset,
|
2022-09-22 04:04:15 +00:00
|
|
|
const CUnorderedMap<uint32_t, uint64_t> *constpoolMap = nullptr);
|
2022-02-06 15:16:08 +00:00
|
|
|
|
2022-08-30 12:52:16 +00:00
|
|
|
void UpdateMainMethodIndex(uint32_t mainMethodIndex, const CString &recordName = ENTRY_FUNCTION_NAME)
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
2022-09-09 08:44:33 +00:00
|
|
|
if (IsBundlePack()) {
|
|
|
|
jsRecordInfo_.begin()->second.mainMethodIndex = mainMethodIndex;
|
|
|
|
} else {
|
|
|
|
auto info = jsRecordInfo_.find(recordName);
|
|
|
|
if (info != jsRecordInfo_.end()) {
|
|
|
|
info->second.mainMethodIndex = mainMethodIndex;
|
|
|
|
}
|
2022-08-30 12:52:16 +00:00
|
|
|
}
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:12:02 +00:00
|
|
|
PUBLIC_API MethodLiteral *FindMethodLiteral(uint32_t offset) const;
|
2022-02-06 15:16:08 +00:00
|
|
|
|
2022-08-30 12:52:16 +00:00
|
|
|
int GetModuleRecordIdx(const CString &recordName = ENTRY_FUNCTION_NAME) const
|
2022-08-26 12:40:41 +00:00
|
|
|
{
|
2022-09-09 08:44:33 +00:00
|
|
|
if (IsBundlePack()) {
|
|
|
|
return jsRecordInfo_.begin()->second.moduleRecordIdx;
|
|
|
|
}
|
2022-08-30 12:52:16 +00:00
|
|
|
auto info = jsRecordInfo_.find(recordName);
|
|
|
|
if (info != jsRecordInfo_.end()) {
|
|
|
|
return info->second.moduleRecordIdx;
|
|
|
|
}
|
|
|
|
// The array subscript will not have a negative number, and returning -1 means the search failed
|
|
|
|
return -1;
|
2022-08-26 12:40:41 +00:00
|
|
|
}
|
2022-02-06 15:16:08 +00:00
|
|
|
|
|
|
|
Span<const uint32_t> GetClasses() const
|
|
|
|
{
|
|
|
|
return pf_->GetClasses();
|
|
|
|
}
|
|
|
|
|
2022-08-30 12:52:16 +00:00
|
|
|
bool PUBLIC_API IsModule(const CString &recordName = ENTRY_FUNCTION_NAME) const;
|
2022-01-30 08:01:33 +00:00
|
|
|
|
2022-08-30 12:52:16 +00:00
|
|
|
bool IsCjs(const CString &recordName = ENTRY_FUNCTION_NAME) const;
|
2022-01-30 08:01:33 +00:00
|
|
|
|
2022-09-09 08:44:33 +00:00
|
|
|
bool IsBundlePack() const
|
2022-08-26 12:40:41 +00:00
|
|
|
{
|
2022-09-09 08:44:33 +00:00
|
|
|
return isBundlePack_;
|
2022-08-26 12:40:41 +00:00
|
|
|
}
|
2022-06-08 02:59:06 +00:00
|
|
|
|
2022-06-08 06:43:48 +00:00
|
|
|
void SetLoadedAOTStatus(bool status)
|
|
|
|
{
|
|
|
|
isLoadedAOT_ = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsLoadedAOT() const
|
|
|
|
{
|
|
|
|
return isLoadedAOT_;
|
|
|
|
}
|
|
|
|
|
2022-06-16 03:21:48 +00:00
|
|
|
uint32_t GetFileUniqId() const
|
|
|
|
{
|
|
|
|
return static_cast<uint32_t>(GetPandaFile()->GetUniqId());
|
|
|
|
}
|
2022-06-16 09:27:35 +00:00
|
|
|
|
2022-08-26 12:40:41 +00:00
|
|
|
bool IsNewVersion() const
|
|
|
|
{
|
|
|
|
return isNewVersion_;
|
|
|
|
}
|
|
|
|
|
2022-08-30 12:52:16 +00:00
|
|
|
bool HasRecord(const CString &recordName) const
|
|
|
|
{
|
|
|
|
auto info = jsRecordInfo_.find(recordName);
|
|
|
|
if (info != jsRecordInfo_.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-09 08:44:33 +00:00
|
|
|
|
2022-10-12 09:22:34 +00:00
|
|
|
JSRecordInfo FindRecordInfo(const CString &recordName) const
|
2022-09-17 06:19:16 +00:00
|
|
|
{
|
|
|
|
auto info = jsRecordInfo_.find(recordName);
|
|
|
|
if (info == jsRecordInfo_.end()) {
|
2022-10-12 09:22:34 +00:00
|
|
|
LOG_FULL(FATAL) << "find recordName failed: " << recordName;
|
2022-09-17 06:19:16 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2022-10-12 09:22:34 +00:00
|
|
|
return info->second;
|
2022-09-17 06:19:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 09:37:33 +00:00
|
|
|
void UpdateHasParsedLiteralConstpool(const CString &recordName)
|
|
|
|
{
|
|
|
|
auto info = jsRecordInfo_.find(recordName);
|
|
|
|
if (info == jsRecordInfo_.end()) {
|
|
|
|
LOG_FULL(FATAL) << "find recordName failed: " << recordName;
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
info->second.hasParsedLiteralConstPool = true;
|
|
|
|
}
|
|
|
|
|
2022-10-12 09:22:34 +00:00
|
|
|
// note : it only uses in TDD
|
|
|
|
void InsertJSRecordInfo(const CString &recordName)
|
2022-09-17 06:19:16 +00:00
|
|
|
{
|
2022-10-12 09:22:34 +00:00
|
|
|
JSRecordInfo info;
|
|
|
|
jsRecordInfo_.insert({recordName, info});
|
2022-09-17 06:19:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 02:19:13 +00:00
|
|
|
const CUnorderedMap<CString, JSRecordInfo> &GetJSRecordInfo() const
|
|
|
|
{
|
|
|
|
return jsRecordInfo_;
|
|
|
|
}
|
2022-10-12 09:22:34 +00:00
|
|
|
|
2022-09-25 13:21:01 +00:00
|
|
|
CString ParseEntryPoint(const CString &recordName) const
|
2022-09-09 08:44:33 +00:00
|
|
|
{
|
2022-10-12 09:22:34 +00:00
|
|
|
return recordName.substr(1, recordName.size() - 2); // 2 : skip symbol "L" and ";"
|
2022-09-09 08:44:33 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 09:22:34 +00:00
|
|
|
void CheckIsBundlePack();
|
2022-09-02 01:31:09 +00:00
|
|
|
|
2022-10-12 09:22:34 +00:00
|
|
|
CString FindEntryPoint(const CString &record) const;
|
2022-09-02 01:31:09 +00:00
|
|
|
|
2022-10-09 10:01:37 +00:00
|
|
|
static CString ParseOhmUrl(const CString &fileName);
|
2022-09-09 03:49:20 +00:00
|
|
|
// For local merge abc, get record name from file name.
|
2022-10-09 10:01:37 +00:00
|
|
|
static CString PUBLIC_API ParseRecordName(const CString &fileName);
|
2022-09-02 01:31:09 +00:00
|
|
|
|
2022-10-19 01:53:32 +00:00
|
|
|
bool IsSystemLib() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-20 12:02:49 +00:00
|
|
|
uint32_t GetAOTFileInfoIndex() const
|
2022-10-19 01:53:32 +00:00
|
|
|
{
|
2022-10-20 12:02:49 +00:00
|
|
|
return anFileInfoIndex_;
|
2022-10-19 01:53:32 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 12:02:49 +00:00
|
|
|
// If the system library is loaded, aotFileInfos has two elements
|
2022-10-19 01:53:32 +00:00
|
|
|
// 0: system library, 1: application
|
2022-10-20 12:02:49 +00:00
|
|
|
// Note: There is no system library currently, so the anFileInfoIndex_ is 0
|
|
|
|
void SetAOTFileInfoIndex()
|
2022-10-19 01:53:32 +00:00
|
|
|
{
|
|
|
|
if (IsSystemLib()) {
|
2022-10-20 12:02:49 +00:00
|
|
|
anFileInfoIndex_ = 0;
|
2022-10-19 01:53:32 +00:00
|
|
|
} else {
|
2022-10-20 12:02:49 +00:00
|
|
|
anFileInfoIndex_ = 1;
|
2022-10-19 01:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 03:08:39 +00:00
|
|
|
static bool IsEntryOrPatch(const CString &name)
|
|
|
|
{
|
|
|
|
return (name == PATCH_FUNCTION_NAME_0) || (name == ENTRY_FUNCTION_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasTSTypes(const CString &recordName) const
|
|
|
|
{
|
|
|
|
JSRecordInfo recordInfo = jsRecordInfo_.at(recordName);
|
|
|
|
return recordInfo.HasTSTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetTypeSummaryOffset(const CString &recordName) const
|
|
|
|
{
|
|
|
|
JSRecordInfo recordInfo = jsRecordInfo_.at(recordName);
|
|
|
|
return recordInfo.GetTypeSummaryOffset();
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:16:08 +00:00
|
|
|
private:
|
2022-08-30 12:52:16 +00:00
|
|
|
void InitializeUnMergedPF();
|
|
|
|
void InitializeMergedPF();
|
2022-08-26 12:40:41 +00:00
|
|
|
|
|
|
|
static constexpr size_t VERSION_SIZE = 4;
|
|
|
|
static constexpr std::array<uint8_t, VERSION_SIZE> OLD_VERSION {0, 0, 0, 2};
|
|
|
|
|
2022-08-31 10:21:38 +00:00
|
|
|
uint32_t constpoolIndex_ {0};
|
2022-09-06 02:19:13 +00:00
|
|
|
CUnorderedMap<uint32_t, MethodLiteral *> methodLiteralMap_;
|
2022-09-22 04:04:15 +00:00
|
|
|
CUnorderedMap<uint32_t, uint64_t> constpoolMap_;
|
2022-02-06 15:16:08 +00:00
|
|
|
uint32_t numMethods_ {0};
|
2022-08-23 01:56:32 +00:00
|
|
|
MethodLiteral *methodLiterals_ {nullptr};
|
2022-02-06 15:16:08 +00:00
|
|
|
const panda_file::File *pf_ {nullptr};
|
2022-03-31 15:38:07 +00:00
|
|
|
CString desc_;
|
2022-06-08 06:43:48 +00:00
|
|
|
bool isLoadedAOT_ {false};
|
2022-10-20 12:02:49 +00:00
|
|
|
uint32_t anFileInfoIndex_ {0};
|
2022-09-20 09:19:03 +00:00
|
|
|
bool isNewVersion_ {false};
|
2022-08-30 12:52:16 +00:00
|
|
|
|
|
|
|
// marge abc
|
2022-09-09 08:44:33 +00:00
|
|
|
bool isBundlePack_ {true}; // isBundlePack means app compile mode is JSBundle
|
2022-08-30 12:52:16 +00:00
|
|
|
CUnorderedMap<CString, JSRecordInfo> jsRecordInfo_;
|
2022-02-06 15:16:08 +00:00
|
|
|
};
|
|
|
|
} // namespace ecmascript
|
|
|
|
} // namespace panda
|
2022-08-31 10:21:38 +00:00
|
|
|
#endif // ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_H
|