2021-09-04 08:06:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 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-06-25 07:25:20 +00:00
|
|
|
#include "ecmascript/js_object-inl.h"
|
2023-08-31 08:51:47 +00:00
|
|
|
#include "pgo_profiler/pgo_profiler_layout.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
namespace panda::ecmascript {
|
2023-08-31 08:51:47 +00:00
|
|
|
using PGOHandler = pgo::PGOHandler;
|
2023-07-12 10:42:42 +00:00
|
|
|
void LayoutInfo::Initialize(const JSThread *thread, int num)
|
|
|
|
{
|
|
|
|
SetExtraLength(num);
|
|
|
|
int propNum = GetPropertiesCapacity();
|
|
|
|
auto attr = PropertyAttributes();
|
|
|
|
for (int i = 0; i < propNum; i++) {
|
|
|
|
SetPropertyInit(thread, i, JSTaggedValue::Hole(), attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 08:35:08 +00:00
|
|
|
void LayoutInfo::GetAllKeys(const JSThread *thread, int end, int offset, TaggedArray *keyArray)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
ASSERT(end <= NumberOfElements());
|
|
|
|
ASSERT_PRINT(offset + end <= static_cast<int>(keyArray->GetLength()),
|
|
|
|
"keyArray capacity is not enough for dictionary");
|
|
|
|
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
int enumKeys = 0;
|
|
|
|
for (int i = 0; i < end; i++) {
|
|
|
|
JSTaggedValue key = GetKey(i);
|
|
|
|
if (key.IsString()) {
|
|
|
|
keyArray->Set(thread, enumKeys + offset, key);
|
|
|
|
enumKeys++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enumKeys < end) {
|
|
|
|
for (int i = 0; i < end; i++) {
|
|
|
|
JSTaggedValue key = GetKey(i);
|
|
|
|
if (key.IsSymbol()) {
|
|
|
|
keyArray->Set(thread, enumKeys + offset, key);
|
|
|
|
enumKeys++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-10 15:12:15 +00:00
|
|
|
void LayoutInfo::GetAllKeysByFilter(const JSThread *thread, uint32_t numberOfProps, uint32_t &keyArrayEffectivelength,
|
2024-11-10 08:35:08 +00:00
|
|
|
TaggedArray *keyArray, uint32_t filter)
|
2023-06-10 15:12:15 +00:00
|
|
|
{
|
2023-06-13 07:27:05 +00:00
|
|
|
ASSERT(numberOfProps <= static_cast<uint32_t>(NumberOfElements()));
|
2023-06-10 15:12:15 +00:00
|
|
|
ASSERT_PRINT(keyArrayEffectivelength + numberOfProps <= keyArray->GetLength(),
|
|
|
|
"keyArray capacity is not enough for dictionary");
|
|
|
|
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
uint32_t enumKeys = 0;
|
|
|
|
for (uint32_t i = 0; i < numberOfProps; i++) {
|
|
|
|
JSTaggedValue key = GetKey(static_cast<int>(i));
|
|
|
|
if (key.IsString() && !(filter & NATIVE_KEY_SKIP_STRINGS)) {
|
|
|
|
PropertyAttributes attr = GetAttr(static_cast<int>(i));
|
|
|
|
bool bIgnore = FilterHelper::IgnoreKeyByFilter<PropertyAttributes>(attr, filter);
|
|
|
|
if (bIgnore) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
keyArray->Set(thread, keyArrayEffectivelength, key);
|
|
|
|
keyArrayEffectivelength++;
|
|
|
|
enumKeys++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enumKeys < numberOfProps) {
|
|
|
|
for (uint32_t i = 0; i < numberOfProps; i++) {
|
|
|
|
JSTaggedValue key = GetKey(static_cast<int>(i));
|
|
|
|
if (key.IsSymbol() && !(filter & NATIVE_KEY_SKIP_SYMBOLS)) {
|
|
|
|
PropertyAttributes attr = GetAttr(static_cast<int>(i));
|
|
|
|
bool bIgnore = FilterHelper::IgnoreKeyByFilter<PropertyAttributes>(attr, filter);
|
|
|
|
if (bIgnore) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
keyArray->Set(thread, keyArrayEffectivelength, key);
|
|
|
|
keyArrayEffectivelength++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2023-09-15 05:47:53 +00:00
|
|
|
void LayoutInfo::GetAllKeysForSerialization(int end, std::vector<JSTaggedValue> &keyVector)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
ASSERT(end <= NumberOfElements());
|
|
|
|
for (int i = 0; i < end; i++) {
|
|
|
|
JSTaggedValue key = GetKey(i);
|
2022-05-28 10:13:10 +00:00
|
|
|
if (key.IsString() || key.IsSymbol()) {
|
2021-09-04 08:06:49 +00:00
|
|
|
keyVector.emplace_back(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 08:35:08 +00:00
|
|
|
std::pair<uint32_t, uint32_t> LayoutInfo::GetNumOfEnumKeys(int end) const
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
ASSERT(end <= NumberOfElements());
|
2023-09-28 09:02:15 +00:00
|
|
|
uint32_t enumKeys = 0;
|
|
|
|
uint32_t shadowKeys = 0;
|
|
|
|
for (int i = 0; i < end; i++) {
|
|
|
|
JSTaggedValue key = GetKey(i);
|
|
|
|
if (!key.IsString()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (GetAttr(i).IsEnumerable()) {
|
|
|
|
enumKeys++;
|
|
|
|
} else {
|
|
|
|
shadowKeys++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_pair(enumKeys, shadowKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutInfo::GetAllEnumKeys(JSThread *thread, int end, int offset, JSHandle<TaggedArray> keyArray,
|
2024-11-10 08:35:08 +00:00
|
|
|
uint32_t *keys, JSHandle<TaggedQueue> shadowQueue, int32_t lastLength)
|
2023-09-28 09:02:15 +00:00
|
|
|
{
|
|
|
|
ASSERT(end <= NumberOfElements());
|
|
|
|
ASSERT_PRINT(offset <= static_cast<int>(keyArray->GetLength()),
|
2021-09-04 08:06:49 +00:00
|
|
|
"keyArray capacity is not enough for dictionary");
|
2023-09-28 09:02:15 +00:00
|
|
|
JSMutableHandle<JSTaggedValue> keyHandle(thread, JSTaggedValue::Undefined());
|
|
|
|
int enumKeys = 0;
|
|
|
|
for (int i = 0; i < end; i++) {
|
|
|
|
keyHandle.Update(GetKey(i));
|
|
|
|
if (!keyHandle->IsString()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (GetAttr(i).IsEnumerable()) {
|
|
|
|
bool isDuplicated = JSObject::IsDepulicateKeys(thread, keyArray, lastLength, shadowQueue, keyHandle);
|
|
|
|
if (isDuplicated) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
keyArray->Set(thread, enumKeys + offset, keyHandle);
|
|
|
|
enumKeys++;
|
|
|
|
} else {
|
|
|
|
TaggedQueue::PushFixedQueue(thread, shadowQueue, keyHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*keys += enumKeys;
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2024-11-10 08:35:08 +00:00
|
|
|
void LayoutInfo::GetAllEnumKeys(JSThread *thread, int end, int offset, JSHandle<TaggedArray> keyArray, uint32_t *keys)
|
2023-09-28 09:02:15 +00:00
|
|
|
{
|
|
|
|
ASSERT(end <= NumberOfElements());
|
|
|
|
ASSERT_PRINT(offset <= static_cast<int>(keyArray->GetLength()),
|
|
|
|
"keyArray capacity is not enough for dictionary");
|
|
|
|
JSMutableHandle<JSTaggedValue> keyHandle(thread, JSTaggedValue::Undefined());
|
2021-09-04 08:06:49 +00:00
|
|
|
int enumKeys = 0;
|
|
|
|
for (int i = 0; i < end; i++) {
|
2023-09-28 09:02:15 +00:00
|
|
|
keyHandle.Update(GetKey(i));
|
|
|
|
if (keyHandle->IsString() && GetAttr(i).IsEnumerable()) {
|
|
|
|
keyArray->Set(thread, enumKeys + offset, keyHandle);
|
2021-09-04 08:06:49 +00:00
|
|
|
enumKeys++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*keys += enumKeys;
|
|
|
|
}
|
|
|
|
|
2024-04-25 07:04:10 +00:00
|
|
|
CString LayoutInfo::GetSymbolKeyString(JSTaggedValue key)
|
|
|
|
{
|
|
|
|
auto symbol = JSSymbol::Cast(key);
|
|
|
|
if (!symbol->HasId()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
auto id = symbol->GetPrivateId();
|
|
|
|
auto symbolDesc = symbol->GetDescription();
|
|
|
|
if (symbolDesc.IsUndefined()) {
|
|
|
|
return ToCString(id);
|
|
|
|
}
|
|
|
|
if (!symbolDesc.IsString()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
CString str = EcmaStringAccessor(symbolDesc).ToCString();
|
|
|
|
if (str != "method") {
|
|
|
|
return "";
|
|
|
|
}
|
2024-11-10 08:35:08 +00:00
|
|
|
return str.append("_").append(ToCString(id));
|
2024-04-25 07:04:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-07 16:37:58 +00:00
|
|
|
void LayoutInfo::DumpFieldIndexByPGO(int index, pgo::HClassLayoutDesc* desc)
|
2023-05-15 08:48:14 +00:00
|
|
|
{
|
|
|
|
auto key = GetKey(index);
|
2024-07-01 13:06:12 +00:00
|
|
|
auto attr = GetAttr(index);
|
|
|
|
SetIsPGODumped(index);
|
|
|
|
TrackType type = attr.GetTrackType();
|
|
|
|
int propertyMeta = attr.GetPropertyMetaData();
|
2023-05-15 08:48:14 +00:00
|
|
|
if (key.IsString()) {
|
|
|
|
auto keyString = EcmaStringAccessor(key).ToCString();
|
2024-04-07 16:37:58 +00:00
|
|
|
desc->InsertKeyAndDesc(keyString, PGOHandler(type, propertyMeta, false));
|
|
|
|
} else if (key.IsSymbol()) {
|
2024-04-25 07:04:10 +00:00
|
|
|
auto keyString = GetSymbolKeyString(key);
|
|
|
|
if (keyString.empty()) {
|
|
|
|
return;
|
2024-04-07 16:37:58 +00:00
|
|
|
}
|
|
|
|
desc->InsertKeyAndDesc(keyString, PGOHandler(type, propertyMeta, true));
|
2023-05-15 08:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-31 06:47:29 +00:00
|
|
|
|
2024-04-07 16:37:58 +00:00
|
|
|
bool LayoutInfo::UpdateFieldIndexByPGO(int index, pgo::HClassLayoutDesc* desc)
|
2023-10-31 06:47:29 +00:00
|
|
|
{
|
|
|
|
auto key = GetKey(index);
|
2024-07-01 13:06:12 +00:00
|
|
|
auto attr = GetAttr(index);
|
|
|
|
if (attr.IsPGODumped()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SetIsPGODumped(index);
|
|
|
|
TrackType type = attr.GetTrackType();
|
|
|
|
int propertyMeta = attr.GetPropertyMetaData();
|
2023-10-31 06:47:29 +00:00
|
|
|
if (key.IsString()) {
|
|
|
|
auto keyString = EcmaStringAccessor(key).ToCString();
|
2024-04-07 16:37:58 +00:00
|
|
|
return desc->UpdateKeyAndDesc(keyString, PGOHandler(type, propertyMeta, false));
|
|
|
|
} else if (key.IsSymbol()) {
|
2024-04-25 07:04:10 +00:00
|
|
|
auto keyString = GetSymbolKeyString(key);
|
|
|
|
if (keyString.empty()) {
|
|
|
|
return false;
|
2024-04-07 16:37:58 +00:00
|
|
|
}
|
|
|
|
return desc->UpdateKeyAndDesc(keyString, PGOHandler(type, propertyMeta, true));
|
2023-10-31 06:47:29 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
} // namespace panda::ecmascript
|