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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ecmascript/jspandafile/js_pandafile.h"
|
2022-07-23 10:33:33 +00:00
|
|
|
|
2022-02-06 15:16:08 +00:00
|
|
|
#include "ecmascript/jspandafile/js_pandafile_manager.h"
|
2022-04-05 13:28:18 +00:00
|
|
|
#include "ecmascript/jspandafile/program_object.h"
|
2022-04-22 03:03:38 +00:00
|
|
|
#include "libpandafile/class_data_accessor-inl.h"
|
2022-02-06 15:16:08 +00:00
|
|
|
|
|
|
|
namespace panda::ecmascript {
|
2023-09-25 13:16:46 +00:00
|
|
|
namespace {
|
|
|
|
const CString OHOS_PKG_ABC_PATH_ROOT = "/ets/"; // abc file always under /ets/ dir in HAP/HSP
|
|
|
|
} // namespace
|
2023-05-08 09:07:03 +00:00
|
|
|
bool JSPandaFile::loadedFirstPandaFile = false;
|
2022-10-09 10:01:37 +00:00
|
|
|
JSPandaFile::JSPandaFile(const panda_file::File *pf, const CString &descriptor)
|
|
|
|
: pf_(pf), desc_(descriptor)
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
2022-03-21 13:19:10 +00:00
|
|
|
ASSERT(pf_ != nullptr);
|
2023-09-18 07:50:38 +00:00
|
|
|
CheckIsBundlePack();
|
|
|
|
if (isBundlePack_) {
|
2022-08-30 12:52:16 +00:00
|
|
|
InitializeUnMergedPF();
|
|
|
|
} else {
|
|
|
|
InitializeMergedPF();
|
|
|
|
}
|
2022-12-13 07:01:05 +00:00
|
|
|
checksum_ = pf->GetHeader()->checksum;
|
2022-09-05 03:38:50 +00:00
|
|
|
isNewVersion_ = pf_->GetHeader()->version > OLD_VERSION;
|
2023-10-23 16:01:12 +00:00
|
|
|
if (!loadedFirstPandaFile && !isBundlePack_) {
|
2023-05-08 09:07:03 +00:00
|
|
|
// Tag the first merged abc to use constant string. The lifetime of this first panda file is the same
|
2023-05-18 08:38:08 +00:00
|
|
|
// as the vm. And make sure the first pandafile is the same at the compile time and runtime.
|
2023-11-28 15:27:00 +00:00
|
|
|
isFirstPandafile_ = false;
|
2023-05-08 09:07:03 +00:00
|
|
|
loadedFirstPandaFile = true;
|
|
|
|
}
|
2022-08-30 12:52:16 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 07:50:38 +00:00
|
|
|
void JSPandaFile::CheckIsBundlePack()
|
2022-08-30 12:52:16 +00:00
|
|
|
{
|
|
|
|
Span<const uint32_t> classIndexes = pf_->GetClasses();
|
|
|
|
for (const uint32_t index : classIndexes) {
|
|
|
|
panda_file::File::EntityId classId(index);
|
|
|
|
if (pf_->IsExternal(classId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
panda_file::ClassDataAccessor cda(*pf_, classId);
|
|
|
|
cda.EnumerateFields([&](panda_file::FieldDataAccessor &fieldAccessor) -> void {
|
|
|
|
panda_file::File::EntityId fieldNameId = fieldAccessor.GetNameId();
|
2022-12-03 14:03:40 +00:00
|
|
|
panda_file::File::StringData sd = GetStringData(fieldNameId);
|
2022-08-30 12:52:16 +00:00
|
|
|
const char *fieldName = utf::Mutf8AsCString(sd.data);
|
|
|
|
if (std::strcmp(IS_COMMON_JS, fieldName) == 0 || std::strcmp(MODULE_RECORD_IDX, fieldName) == 0) {
|
2023-09-18 07:50:38 +00:00
|
|
|
isBundlePack_ = false;
|
2022-08-30 12:52:16 +00:00
|
|
|
}
|
|
|
|
});
|
2023-09-18 07:50:38 +00:00
|
|
|
if (!isBundlePack_) {
|
2022-08-30 12:52:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 10:52:10 +00:00
|
|
|
void JSPandaFile::CheckIsRecordWithBundleName(const CString &entry)
|
2023-01-11 11:24:44 +00:00
|
|
|
{
|
2023-05-15 10:52:10 +00:00
|
|
|
size_t pos = entry.find('/');
|
|
|
|
if (pos == CString::npos) {
|
|
|
|
LOG_FULL(FATAL) << "CheckIsRecordWithBundleName Invalid parameter entry";
|
|
|
|
}
|
|
|
|
|
|
|
|
CString bundleName = entry.substr(0, pos);
|
|
|
|
size_t bundleNameLen = bundleName.length();
|
2023-01-12 07:34:13 +00:00
|
|
|
for (auto info : jsRecordInfo_) {
|
2023-02-02 08:08:18 +00:00
|
|
|
if (info.first.find(PACKAGE_PATH_SEGMENT) != CString::npos ||
|
|
|
|
info.first.find(NPM_PATH_SEGMENT) != CString::npos) {
|
2023-01-12 07:34:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CString recordName = info.first;
|
|
|
|
// Confirm whether the current record is new or old by judging whether the recordName has a bundleName
|
2023-03-23 08:18:06 +00:00
|
|
|
if (!(recordName.length() > bundleNameLen && (recordName.compare(0, bundleNameLen, bundleName) == 0))) {
|
|
|
|
isRecordWithBundleName_ = false;
|
2023-01-12 07:34:13 +00:00
|
|
|
}
|
|
|
|
return;
|
2023-01-11 11:24:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 15:16:08 +00:00
|
|
|
JSPandaFile::~JSPandaFile()
|
|
|
|
{
|
|
|
|
if (pf_ != nullptr) {
|
|
|
|
delete pf_;
|
|
|
|
pf_ = nullptr;
|
|
|
|
}
|
2023-01-09 07:19:40 +00:00
|
|
|
|
|
|
|
constpoolMap_.clear();
|
2022-09-08 06:14:50 +00:00
|
|
|
jsRecordInfo_.clear();
|
2022-08-27 03:52:15 +00:00
|
|
|
methodLiteralMap_.clear();
|
2023-12-16 08:08:09 +00:00
|
|
|
ClearNameMap();
|
2022-08-23 01:56:32 +00:00
|
|
|
if (methodLiterals_ != nullptr) {
|
|
|
|
JSPandaFileManager::FreeBuffer(methodLiterals_);
|
|
|
|
methodLiterals_ = nullptr;
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 04:04:15 +00:00
|
|
|
uint32_t JSPandaFile::GetOrInsertConstantPool(ConstPoolType type, uint32_t offset,
|
|
|
|
const CUnorderedMap<uint32_t, uint64_t> *constpoolMap)
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
2022-09-22 04:04:15 +00:00
|
|
|
CUnorderedMap<uint32_t, uint64_t> *map = nullptr;
|
2023-09-18 07:50:38 +00:00
|
|
|
if (constpoolMap != nullptr && !IsBundlePack()) {
|
2022-09-22 04:04:15 +00:00
|
|
|
map = const_cast<CUnorderedMap<uint32_t, uint64_t> *>(constpoolMap);
|
|
|
|
} else {
|
|
|
|
map = &constpoolMap_;
|
2022-09-09 08:44:33 +00:00
|
|
|
}
|
2022-09-22 04:04:15 +00:00
|
|
|
auto it = map->find(offset);
|
|
|
|
if (it != map->cend()) {
|
2022-09-09 08:44:33 +00:00
|
|
|
ConstPoolValue value(it->second);
|
|
|
|
return value.GetConstpoolIndex();
|
2022-02-28 07:04:37 +00:00
|
|
|
}
|
2022-09-09 08:44:33 +00:00
|
|
|
ASSERT(constpoolIndex_ != UINT32_MAX);
|
|
|
|
uint32_t index = constpoolIndex_++;
|
|
|
|
ConstPoolValue value(type, index);
|
2022-09-23 01:40:20 +00:00
|
|
|
map->emplace(offset, value.GetValue());
|
2022-09-09 08:44:33 +00:00
|
|
|
return index;
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 12:52:16 +00:00
|
|
|
void JSPandaFile::InitializeUnMergedPF()
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
|
|
|
Span<const uint32_t> classIndexes = pf_->GetClasses();
|
2022-08-30 12:52:16 +00:00
|
|
|
JSRecordInfo info;
|
2022-02-06 15:16:08 +00:00
|
|
|
for (const uint32_t index : classIndexes) {
|
|
|
|
panda_file::File::EntityId classId(index);
|
|
|
|
if (pf_->IsExternal(classId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
panda_file::ClassDataAccessor cda(*pf_, classId);
|
|
|
|
numMethods_ += cda.GetMethodsNumber();
|
2022-01-30 08:01:33 +00:00
|
|
|
const char *desc = utf::Mutf8AsCString(cda.GetDescriptor());
|
2022-08-30 12:52:16 +00:00
|
|
|
if (info.moduleRecordIdx == -1 && std::strcmp(MODULE_CLASS, desc) == 0) {
|
2022-11-29 02:22:20 +00:00
|
|
|
cda.EnumerateFields([&](panda_file::FieldDataAccessor &fieldAccessor) -> void {
|
|
|
|
panda_file::File::EntityId fieldNameId = fieldAccessor.GetNameId();
|
2022-12-03 14:03:40 +00:00
|
|
|
panda_file::File::StringData sd = GetStringData(fieldNameId);
|
2023-01-09 07:19:40 +00:00
|
|
|
CString fieldName = utf::Mutf8AsCString(sd.data);
|
|
|
|
if (fieldName != desc_) {
|
2022-11-29 02:22:20 +00:00
|
|
|
info.moduleRecordIdx = fieldAccessor.GetValue<int32_t>().value();
|
2023-11-11 10:52:15 +00:00
|
|
|
info.classId = index;
|
2022-08-30 12:52:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2022-01-30 08:01:33 +00:00
|
|
|
}
|
2022-08-30 12:52:16 +00:00
|
|
|
if (!info.isCjs && std::strcmp(COMMONJS_CLASS, desc) == 0) {
|
2023-11-11 10:52:15 +00:00
|
|
|
info.classId = index;
|
2022-08-30 12:52:16 +00:00
|
|
|
info.isCjs = true;
|
2022-06-08 02:59:06 +00:00
|
|
|
}
|
2023-12-09 14:26:03 +00:00
|
|
|
if (!info.hasTopLevelAwait && std::strcmp(HASTLA_CLASS, desc) == 0) {
|
|
|
|
info.hasTopLevelAwait = true;
|
|
|
|
}
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
2022-09-09 08:44:33 +00:00
|
|
|
jsRecordInfo_.insert({JSPandaFile::ENTRY_FUNCTION_NAME, info});
|
2022-08-30 12:52:16 +00:00
|
|
|
methodLiterals_ =
|
|
|
|
static_cast<MethodLiteral *>(JSPandaFileManager::AllocateBuffer(sizeof(MethodLiteral) * numMethods_));
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSPandaFile::InitializeMergedPF()
|
|
|
|
{
|
|
|
|
Span<const uint32_t> classIndexes = pf_->GetClasses();
|
|
|
|
for (const uint32_t index : classIndexes) {
|
|
|
|
panda_file::File::EntityId classId(index);
|
|
|
|
if (pf_->IsExternal(classId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
panda_file::ClassDataAccessor cda(*pf_, classId);
|
|
|
|
numMethods_ += cda.GetMethodsNumber();
|
|
|
|
// get record info
|
|
|
|
JSRecordInfo info;
|
2023-11-11 10:52:15 +00:00
|
|
|
info.classId = index;
|
2022-09-09 08:44:33 +00:00
|
|
|
bool hasCjsFiled = false;
|
2022-12-26 08:05:37 +00:00
|
|
|
bool hasJsonFiled = false;
|
2022-08-30 12:52:16 +00:00
|
|
|
cda.EnumerateFields([&](panda_file::FieldDataAccessor &fieldAccessor) -> void {
|
|
|
|
panda_file::File::EntityId fieldNameId = fieldAccessor.GetNameId();
|
2022-12-03 14:03:40 +00:00
|
|
|
panda_file::File::StringData sd = GetStringData(fieldNameId);
|
2022-08-30 12:52:16 +00:00
|
|
|
const char *fieldName = utf::Mutf8AsCString(sd.data);
|
|
|
|
if (std::strcmp(IS_COMMON_JS, fieldName) == 0) {
|
2022-09-09 08:44:33 +00:00
|
|
|
hasCjsFiled = true;
|
2022-08-30 12:52:16 +00:00
|
|
|
info.isCjs = fieldAccessor.GetValue<bool>().value();
|
2022-12-26 08:05:37 +00:00
|
|
|
} else if (std::strcmp(IS_JSON_CONTENT, fieldName) == 0) {
|
|
|
|
hasJsonFiled = true;
|
|
|
|
info.isJson = true;
|
|
|
|
info.jsonStringId = fieldAccessor.GetValue<uint32_t>().value();
|
2022-10-20 03:08:39 +00:00
|
|
|
} else if (std::strcmp(MODULE_RECORD_IDX, fieldName) == 0) {
|
|
|
|
info.moduleRecordIdx = fieldAccessor.GetValue<int32_t>().value();
|
2023-12-09 14:26:03 +00:00
|
|
|
} else if (std::strcmp(HAS_TOP_LEVEL_AWAIT, fieldName) == 0) {
|
|
|
|
info.hasTopLevelAwait = fieldAccessor.GetValue<bool>().value();
|
2022-10-20 03:08:39 +00:00
|
|
|
} else if (std::strcmp(TYPE_FLAG, fieldName) == 0) {
|
|
|
|
info.hasTSTypes = fieldAccessor.GetValue<uint8_t>().value() != 0;
|
|
|
|
} else if (std::strcmp(TYPE_SUMMARY_OFFSET, fieldName) == 0) {
|
|
|
|
info.typeSummaryOffset = fieldAccessor.GetValue<uint32_t>().value();
|
2022-12-01 03:13:10 +00:00
|
|
|
} else if (std::strlen(fieldName) > PACKAGE_NAME_LEN &&
|
|
|
|
std::strncmp(fieldName, PACKAGE_NAME, PACKAGE_NAME_LEN) == 0) {
|
|
|
|
info.npmPackageName = fieldName + PACKAGE_NAME_LEN;
|
2022-08-30 12:52:16 +00:00
|
|
|
}
|
|
|
|
});
|
2022-12-26 08:05:37 +00:00
|
|
|
if (hasCjsFiled || hasJsonFiled) {
|
2023-01-09 07:19:40 +00:00
|
|
|
CString desc = utf::Mutf8AsCString(cda.GetDescriptor());
|
2022-09-09 08:44:33 +00:00
|
|
|
jsRecordInfo_.insert({ParseEntryPoint(desc), info});
|
|
|
|
}
|
2022-08-30 12:52:16 +00:00
|
|
|
}
|
2022-08-23 01:56:32 +00:00
|
|
|
methodLiterals_ =
|
|
|
|
static_cast<MethodLiteral *>(JSPandaFileManager::AllocateBuffer(sizeof(MethodLiteral) * numMethods_));
|
2022-06-08 02:59:06 +00:00
|
|
|
}
|
2022-02-06 15:16:08 +00:00
|
|
|
|
2022-08-27 03:52:15 +00:00
|
|
|
MethodLiteral *JSPandaFile::FindMethodLiteral(uint32_t offset) const
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
2022-09-02 01:17:30 +00:00
|
|
|
auto iter = methodLiteralMap_.find(offset);
|
|
|
|
if (iter == methodLiteralMap_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return iter->second;
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
2022-01-30 08:01:33 +00:00
|
|
|
|
2023-05-08 09:07:03 +00:00
|
|
|
bool JSPandaFile::IsFirstMergedAbc() const
|
|
|
|
{
|
2023-09-18 07:50:38 +00:00
|
|
|
if (isFirstPandafile_ && !IsBundlePack()) {
|
2023-05-08 09:07:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-18 11:32:21 +00:00
|
|
|
bool JSPandaFile::CheckAndGetRecordInfo(const CString &recordName, JSRecordInfo &recordInfo) const
|
2022-01-30 08:01:33 +00:00
|
|
|
{
|
2023-09-18 07:50:38 +00:00
|
|
|
if (IsBundlePack()) {
|
2023-07-18 11:32:21 +00:00
|
|
|
recordInfo = jsRecordInfo_.begin()->second;
|
|
|
|
return true;
|
2022-12-26 08:05:37 +00:00
|
|
|
}
|
|
|
|
auto info = jsRecordInfo_.find(recordName);
|
|
|
|
if (info != jsRecordInfo_.end()) {
|
2023-07-18 11:32:21 +00:00
|
|
|
recordInfo = info->second;
|
|
|
|
return true;
|
2022-12-26 08:05:37 +00:00
|
|
|
}
|
2023-07-18 11:32:21 +00:00
|
|
|
return false;
|
2022-12-26 08:05:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 08:48:37 +00:00
|
|
|
CString JSPandaFile::GetEntryPoint(const CString &recordName) const
|
2022-08-30 12:52:16 +00:00
|
|
|
{
|
2023-12-27 07:43:42 +00:00
|
|
|
CString entryPoint;
|
|
|
|
if (FindOhmUrlInPF(recordName, entryPoint) && HasRecord(entryPoint)) {
|
2023-03-16 08:48:37 +00:00
|
|
|
return entryPoint;
|
2022-12-01 03:13:10 +00:00
|
|
|
}
|
2023-03-16 08:48:37 +00:00
|
|
|
return CString();
|
|
|
|
}
|
|
|
|
|
2023-12-27 07:43:42 +00:00
|
|
|
bool JSPandaFile::FindOhmUrlInPF(const CString &recordName, CString &entryPoint) const
|
2023-03-16 08:48:37 +00:00
|
|
|
{
|
2022-08-30 12:52:16 +00:00
|
|
|
Span<const uint32_t> classIndexes = pf_->GetClasses();
|
|
|
|
for (const uint32_t index : classIndexes) {
|
|
|
|
panda_file::File::EntityId classId(index);
|
|
|
|
if (pf_->IsExternal(classId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
panda_file::ClassDataAccessor cda(*pf_, classId);
|
2022-09-09 08:44:33 +00:00
|
|
|
CString desc = utf::Mutf8AsCString(cda.GetDescriptor());
|
2023-01-09 07:19:40 +00:00
|
|
|
if (recordName == ParseEntryPoint(desc)) {
|
2022-08-30 12:52:16 +00:00
|
|
|
cda.EnumerateFields([&](panda_file::FieldDataAccessor &fieldAccessor) -> void {
|
|
|
|
panda_file::File::EntityId fieldNameId = fieldAccessor.GetNameId();
|
2022-12-03 14:03:40 +00:00
|
|
|
panda_file::File::StringData sd = GetStringData(fieldNameId);
|
2022-10-12 09:22:34 +00:00
|
|
|
CString fieldName = utf::Mutf8AsCString(sd.data);
|
2023-12-27 07:43:42 +00:00
|
|
|
entryPoint = fieldName;
|
2022-08-30 12:52:16 +00:00
|
|
|
});
|
|
|
|
}
|
2023-12-27 07:43:42 +00:00
|
|
|
if (!entryPoint.empty()) {
|
|
|
|
return true;
|
2022-08-30 12:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-27 07:43:42 +00:00
|
|
|
return false;
|
2022-08-30 12:52:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 08:25:33 +00:00
|
|
|
FunctionKind JSPandaFile::GetFunctionKind(panda_file::FunctionKind funcKind)
|
|
|
|
{
|
|
|
|
FunctionKind kind;
|
|
|
|
switch (funcKind) {
|
|
|
|
case panda_file::FunctionKind::NONE:
|
2023-12-24 08:21:26 +00:00
|
|
|
kind = FunctionKind::NONE_FUNCTION;
|
|
|
|
break;
|
2022-11-15 08:25:33 +00:00
|
|
|
case panda_file::FunctionKind::FUNCTION:
|
|
|
|
kind = FunctionKind::BASE_CONSTRUCTOR;
|
|
|
|
break;
|
|
|
|
case panda_file::FunctionKind::NC_FUNCTION:
|
|
|
|
kind = FunctionKind::ARROW_FUNCTION;
|
|
|
|
break;
|
|
|
|
case panda_file::FunctionKind::GENERATOR_FUNCTION:
|
|
|
|
kind = FunctionKind::GENERATOR_FUNCTION;
|
|
|
|
break;
|
|
|
|
case panda_file::FunctionKind::ASYNC_FUNCTION:
|
|
|
|
kind = FunctionKind::ASYNC_FUNCTION;
|
|
|
|
break;
|
|
|
|
case panda_file::FunctionKind::ASYNC_GENERATOR_FUNCTION:
|
|
|
|
kind = FunctionKind::ASYNC_GENERATOR_FUNCTION;
|
|
|
|
break;
|
|
|
|
case panda_file::FunctionKind::ASYNC_NC_FUNCTION:
|
|
|
|
kind = FunctionKind::ASYNC_ARROW_FUNCTION;
|
|
|
|
break;
|
2023-01-02 09:37:02 +00:00
|
|
|
case panda_file::FunctionKind::CONCURRENT_FUNCTION:
|
|
|
|
kind = FunctionKind::CONCURRENT_FUNCTION;
|
|
|
|
break;
|
2022-11-15 08:25:33 +00:00
|
|
|
default:
|
2022-12-01 07:29:41 +00:00
|
|
|
LOG_ECMA(FATAL) << "this branch is unreachable";
|
2022-11-15 08:25:33 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
return kind;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionKind JSPandaFile::GetFunctionKind(ConstPoolType type)
|
|
|
|
{
|
|
|
|
FunctionKind kind;
|
|
|
|
switch (type) {
|
|
|
|
case ConstPoolType::BASE_FUNCTION:
|
|
|
|
kind = FunctionKind::BASE_CONSTRUCTOR;
|
|
|
|
break;
|
|
|
|
case ConstPoolType::NC_FUNCTION:
|
|
|
|
kind = FunctionKind::ARROW_FUNCTION;
|
|
|
|
break;
|
|
|
|
case ConstPoolType::GENERATOR_FUNCTION:
|
|
|
|
kind = FunctionKind::GENERATOR_FUNCTION;
|
|
|
|
break;
|
|
|
|
case ConstPoolType::ASYNC_FUNCTION:
|
|
|
|
kind = FunctionKind::ASYNC_FUNCTION;
|
|
|
|
break;
|
|
|
|
case ConstPoolType::CLASS_FUNCTION:
|
|
|
|
kind = FunctionKind::CLASS_CONSTRUCTOR;
|
|
|
|
break;
|
|
|
|
case ConstPoolType::METHOD:
|
|
|
|
kind = FunctionKind::NORMAL_FUNCTION;
|
|
|
|
break;
|
|
|
|
case ConstPoolType::ASYNC_GENERATOR_FUNCTION:
|
|
|
|
kind = FunctionKind::ASYNC_GENERATOR_FUNCTION;
|
|
|
|
break;
|
|
|
|
default:
|
2022-12-01 07:29:41 +00:00
|
|
|
LOG_ECMA(FATAL) << "this branch is unreachable";
|
2022-11-15 08:25:33 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
return kind;
|
|
|
|
}
|
2023-09-25 13:16:46 +00:00
|
|
|
|
|
|
|
/*
|
2023-10-26 09:22:46 +00:00
|
|
|
handle desc like: case1: /data/storage/el1/bundle/entry/ets/modules.abc -> entry/ets/modules.abc
|
2023-09-25 13:16:46 +00:00
|
|
|
case2: /data/storage/el1/bundle/entry/ets/widgets.abc -> entry/ets/widgets.abc
|
|
|
|
case3: /data/app/el1/bundle/public/com.xx.xx/entry/ets/modules.abc -> entry/ets/modules.abc
|
|
|
|
case4: /data/app/el1/bundle/public/com.xx.xx/entry/ets/widgets.abc -> entry/ets/widgets.abc
|
|
|
|
*/
|
|
|
|
CString JSPandaFile::GetNormalizedFileDesc(const CString &desc)
|
|
|
|
{
|
|
|
|
auto etsTokenPos = desc.rfind(OHOS_PKG_ABC_PATH_ROOT);
|
|
|
|
if (etsTokenPos == std::string::npos) {
|
|
|
|
// file not in OHOS package.
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
auto ohosModulePos = desc.rfind('/', etsTokenPos - 1);
|
|
|
|
if (ohosModulePos == std::string::npos) {
|
|
|
|
LOG_ECMA(ERROR) << "Get abcPath from desc failed. desc: " << desc;
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
// substring likes {ohosModuleName}/ets/modules.abc or {ohosModuleName}/ets/widgets.abc
|
|
|
|
return desc.substr(ohosModulePos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
CString JSPandaFile::GetNormalizedFileDesc() const
|
|
|
|
{
|
|
|
|
return GetNormalizedFileDesc(desc_);
|
|
|
|
}
|
2023-12-16 08:08:09 +00:00
|
|
|
|
|
|
|
const char *JSPandaFile::GetMethodName(EntityId methodId)
|
|
|
|
{
|
2024-01-11 12:19:37 +00:00
|
|
|
LockHolder lock(methodNameMapMutex_);
|
2023-12-16 08:08:09 +00:00
|
|
|
uint32_t id = methodId.GetOffset();
|
|
|
|
auto iter = methodNameMap_.find(id);
|
|
|
|
if (iter != methodNameMap_.end()) {
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
panda_file::MethodDataAccessor mda(*pf_, methodId);
|
|
|
|
auto sd = GetStringData(mda.GetNameId());
|
|
|
|
auto name = utf::Mutf8AsCString(sd.data);
|
|
|
|
methodNameMap_.emplace(id, name);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2024-01-23 02:08:13 +00:00
|
|
|
const char *JSPandaFile::GetCpuProfilerMethodName(EntityId methodId)
|
|
|
|
{
|
|
|
|
panda_file::MethodDataAccessor mda(*pf_, methodId);
|
|
|
|
auto sd = GetStringData(mda.GetNameId());
|
|
|
|
auto name = utf::Mutf8AsCString(sd.data);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2023-12-16 08:08:09 +00:00
|
|
|
CString JSPandaFile::GetRecordName(EntityId methodId)
|
|
|
|
{
|
2024-01-11 12:19:37 +00:00
|
|
|
LockHolder lock(recordNameMapMutex_);
|
2023-12-16 08:08:09 +00:00
|
|
|
uint32_t id = methodId.GetOffset();
|
|
|
|
auto iter = recordNameMap_.find(id);
|
|
|
|
if (iter != recordNameMap_.end()) {
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
panda_file::MethodDataAccessor mda(*pf_, methodId);
|
|
|
|
panda_file::ClassDataAccessor cda(*pf_, mda.GetClassId());
|
|
|
|
CString desc = utf::Mutf8AsCString(cda.GetDescriptor());
|
|
|
|
auto name = JSPandaFile::ParseEntryPoint(desc);
|
|
|
|
recordNameMap_.emplace(id, name);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSPandaFile::ClearNameMap()
|
|
|
|
{
|
2024-01-11 12:19:37 +00:00
|
|
|
{
|
|
|
|
LockHolder lock(methodNameMapMutex_);
|
|
|
|
methodNameMap_.clear();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LockHolder lock(recordNameMapMutex_);
|
|
|
|
recordNameMap_.clear();
|
|
|
|
}
|
2023-12-16 08:08:09 +00:00
|
|
|
}
|
2022-02-06 15:16:08 +00:00
|
|
|
} // namespace panda::ecmascript
|