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-05-11 09:36:28 +00:00
|
|
|
#include "ecmascript/snapshot/mem/snapshot.h"
|
|
|
|
#include "ecmascript/snapshot/mem/snapshot_processor.h"
|
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-07-23 10:33:33 +00:00
|
|
|
|
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 {
|
2022-03-31 15:38:07 +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);
|
2022-01-30 08:01:33 +00:00
|
|
|
Initialize();
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSPandaFile::~JSPandaFile()
|
|
|
|
{
|
|
|
|
if (pf_ != nullptr) {
|
|
|
|
delete pf_;
|
|
|
|
pf_ = nullptr;
|
|
|
|
}
|
2022-03-10 09:02:04 +00:00
|
|
|
methodMap_.clear();
|
2022-02-06 15:16:08 +00:00
|
|
|
if (methods_ != nullptr) {
|
|
|
|
JSPandaFileManager::FreeBuffer(methods_);
|
|
|
|
methods_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:04:37 +00:00
|
|
|
uint32_t JSPandaFile::GetOrInsertConstantPool(ConstPoolType type, uint32_t offset)
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
2022-02-28 07:04:37 +00:00
|
|
|
auto it = constpoolMap_.find(offset);
|
|
|
|
if (it != constpoolMap_.cend()) {
|
|
|
|
ConstPoolValue value(it->second);
|
|
|
|
return value.GetConstpoolIndex();
|
|
|
|
}
|
|
|
|
ASSERT(constpoolIndex_ != UINT32_MAX);
|
|
|
|
uint32_t index = constpoolIndex_++;
|
|
|
|
ConstPoolValue value(type, index);
|
|
|
|
constpoolMap_.insert({offset, value.GetValue()});
|
|
|
|
return index;
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 08:01:33 +00:00
|
|
|
void JSPandaFile::Initialize()
|
2022-02-06 15:16:08 +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);
|
|
|
|
numMethods_ += cda.GetMethodsNumber();
|
2022-01-30 08:01:33 +00:00
|
|
|
const char *desc = utf::Mutf8AsCString(cda.GetDescriptor());
|
|
|
|
if (!isModule_ && std::strcmp(MODULE_CLASS, desc) == 0) {
|
|
|
|
isModule_ = true;
|
|
|
|
}
|
2022-06-08 02:59:06 +00:00
|
|
|
if (std::strcmp(COMMONJS_CLASS, desc) == 0) {
|
|
|
|
isCjs_ = true;
|
|
|
|
}
|
2022-04-18 04:39:12 +00:00
|
|
|
|
2022-04-25 13:23:42 +00:00
|
|
|
if (!HasTSTypes() && std::strcmp(TS_TYPES_CLASS, desc) == 0) {
|
2022-04-18 04:39:12 +00:00
|
|
|
cda.EnumerateFields([&](panda_file::FieldDataAccessor &fieldAccessor) -> void {
|
|
|
|
panda_file::File::EntityId fieldNameId = fieldAccessor.GetNameId();
|
2022-04-22 03:03:38 +00:00
|
|
|
panda_file::File::StringData sd = pf_->GetStringData(fieldNameId);
|
2022-04-18 04:39:12 +00:00
|
|
|
const char *fieldName = utf::Mutf8AsCString(sd.data);
|
|
|
|
if (std::strcmp(TYPE_FLAG, fieldName) == 0) {
|
|
|
|
hasTSTypes_ = fieldAccessor.GetValue<uint8_t>().value() != 0;
|
|
|
|
}
|
|
|
|
if (std::strcmp(TYPE_SUMMARY_INDEX, fieldName) == 0) {
|
|
|
|
typeSummaryIndex_ = fieldAccessor.GetValue<uint32_t>().value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
2022-08-16 13:05:02 +00:00
|
|
|
methods_ = static_cast<MethodLiteral *>(JSPandaFileManager::AllocateBuffer(sizeof(MethodLiteral) * numMethods_));
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 13:05:02 +00:00
|
|
|
MethodLiteral *JSPandaFile::FindMethods(uint32_t offset) const
|
2022-02-06 15:16:08 +00:00
|
|
|
{
|
2022-03-10 09:02:04 +00:00
|
|
|
return methodMap_.at(offset);
|
2022-02-06 15:16:08 +00:00
|
|
|
}
|
2022-01-30 08:01:33 +00:00
|
|
|
|
|
|
|
bool JSPandaFile::IsModule() const
|
|
|
|
{
|
|
|
|
return isModule_;
|
|
|
|
}
|
2022-06-08 02:59:06 +00:00
|
|
|
|
|
|
|
bool JSPandaFile::IsCjs() const
|
|
|
|
{
|
|
|
|
return isCjs_;
|
|
|
|
}
|
2022-02-06 15:16:08 +00:00
|
|
|
} // namespace panda::ecmascript
|