2021-09-04 08:06:49 +00:00
|
|
|
/*
|
2024-07-11 13:21:48 +00:00
|
|
|
* Copyright (c) 2021-2024 Huawei Device Co., Ltd.
|
2021-09-04 08:06:49 +00:00
|
|
|
* 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 <algorithm>
|
|
|
|
|
2024-07-11 13:21:48 +00:00
|
|
|
#include "ecmascript/ecma_context.h"
|
|
|
|
#include "ecmascript/global_env_constants-inl.h"
|
2023-08-21 12:11:01 +00:00
|
|
|
#include "ecmascript/pgo_profiler/pgo_profiler.h"
|
2023-10-31 06:47:29 +00:00
|
|
|
#include "ecmascript/pgo_profiler/pgo_profiler_layout.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/ic/proto_change_details.h"
|
2024-07-11 13:21:48 +00:00
|
|
|
#include "ecmascript/js_function.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/js_object-inl.h"
|
|
|
|
|
|
|
|
namespace panda::ecmascript {
|
2024-04-09 23:24:36 +00:00
|
|
|
using ProfileType = pgo::ProfileType;
|
|
|
|
|
2021-12-17 09:18:10 +00:00
|
|
|
JSHandle<TransitionsDictionary> TransitionsDictionary::PutIfAbsent(const JSThread *thread,
|
|
|
|
const JSHandle<TransitionsDictionary> &dictionary,
|
|
|
|
const JSHandle<JSTaggedValue> &key,
|
|
|
|
const JSHandle<JSTaggedValue> &value,
|
|
|
|
const JSHandle<JSTaggedValue> &metaData)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2024-08-10 03:36:57 +00:00
|
|
|
uint32_t hash = static_cast<uint32_t>(TransitionsDictionary::Hash(key.GetTaggedValue(), metaData.GetTaggedValue()));
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
/* no need to add key if exist */
|
|
|
|
int entry = dictionary->FindEntry(key.GetTaggedValue(), metaData.GetTaggedValue());
|
|
|
|
if (entry != -1) {
|
2022-03-20 07:02:45 +00:00
|
|
|
if (dictionary->GetValue(entry).IsUndefined()) {
|
|
|
|
JSTaggedValue weakValue = JSTaggedValue(value->CreateAndGetWeakRef());
|
|
|
|
dictionary->SetValue(thread, entry, weakValue);
|
|
|
|
}
|
2021-12-17 09:18:10 +00:00
|
|
|
return dictionary;
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the dictionary should be extended.
|
2021-12-17 09:18:10 +00:00
|
|
|
JSHandle<TransitionsDictionary> newDictionary(HashTableT::GrowHashTable(thread, dictionary));
|
2021-09-04 08:06:49 +00:00
|
|
|
// Compute the key object.
|
|
|
|
entry = newDictionary->FindInsertIndex(hash);
|
2022-03-11 09:30:43 +00:00
|
|
|
JSTaggedValue val = value.GetTaggedValue();
|
|
|
|
newDictionary->SetEntry(thread, entry, key.GetTaggedValue(), val, metaData.GetTaggedValue());
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
newDictionary->IncreaseEntries(thread);
|
|
|
|
return newDictionary;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TransitionsDictionary::FindEntry(const JSTaggedValue &key, const JSTaggedValue &metaData)
|
|
|
|
{
|
2022-04-12 08:49:37 +00:00
|
|
|
size_t size = static_cast<size_t>(Size());
|
2021-09-07 14:24:16 +00:00
|
|
|
uint32_t count = 1;
|
2022-04-12 08:49:37 +00:00
|
|
|
int32_t hash = TransitionsDictionary::Hash(key, metaData);
|
2021-09-04 08:06:49 +00:00
|
|
|
// GrowHashTable will guarantee the hash table is never full.
|
2022-04-12 08:49:37 +00:00
|
|
|
for (uint32_t entry = GetFirstPosition(hash, size);; entry = GetNextPosition(entry, count++, size)) {
|
2021-09-04 08:06:49 +00:00
|
|
|
JSTaggedValue element = GetKey(entry);
|
|
|
|
if (element.IsHole()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (element.IsUndefined()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-03-01 11:26:53 +00:00
|
|
|
if (TransitionsDictionary::IsMatch(key, metaData, element, GetAttributes(entry).GetWeakRawValue())) {
|
2022-04-12 08:49:37 +00:00
|
|
|
return static_cast<int>(entry);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:18:10 +00:00
|
|
|
JSHandle<TransitionsDictionary> TransitionsDictionary::Remove(const JSThread *thread,
|
|
|
|
const JSHandle<TransitionsDictionary> &table,
|
|
|
|
const JSHandle<JSTaggedValue> &key,
|
|
|
|
const JSTaggedValue &metaData)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
int entry = table->FindEntry(key.GetTaggedValue(), metaData);
|
|
|
|
if (entry == -1) {
|
2021-12-17 09:18:10 +00:00
|
|
|
return table;
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
table->RemoveElement(thread, entry);
|
|
|
|
return TransitionsDictionary::Shrink(thread, table);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransitionsDictionary::Rehash(const JSThread *thread, TransitionsDictionary *newTable)
|
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
2023-03-01 11:26:53 +00:00
|
|
|
if (newTable == nullptr) {
|
2021-09-04 08:06:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int size = this->Size();
|
|
|
|
// Rehash elements to new table
|
2023-03-01 11:26:53 +00:00
|
|
|
int entryCount = 0;
|
2021-09-04 08:06:49 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
int fromIndex = GetEntryIndex(i);
|
|
|
|
JSTaggedValue k = this->GetKey(i);
|
2023-03-01 11:26:53 +00:00
|
|
|
JSTaggedValue v = this->GetValue(i);
|
|
|
|
if (IsKey(k) && TransitionsDictionary::CheckWeakExist(v)) {
|
2024-08-10 03:36:57 +00:00
|
|
|
uint32_t hash = static_cast<uint32_t>(TransitionsDictionary::Hash(k, this->GetAttributes(i)));
|
2023-03-01 11:26:53 +00:00
|
|
|
int insertionIndex = GetEntryIndex(newTable->FindInsertIndex(hash));
|
|
|
|
JSTaggedValue tv = Get(fromIndex);
|
|
|
|
newTable->Set(thread, insertionIndex, tv);
|
|
|
|
for (int j = 1; j < TransitionsDictionary::ENTRY_SIZE; j++) {
|
|
|
|
tv = Get(fromIndex + j);
|
|
|
|
newTable->Set(thread, insertionIndex + j, tv);
|
|
|
|
}
|
|
|
|
entryCount++;
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-01 11:26:53 +00:00
|
|
|
newTable->SetEntriesCount(thread, entryCount);
|
2021-09-04 08:06:49 +00:00
|
|
|
newTable->SetHoleEntriesCount(thread, 0);
|
|
|
|
}
|
|
|
|
|
2024-01-23 13:45:43 +00:00
|
|
|
void JSHClass::InitializeWithDefaultValue(const JSThread *thread, uint32_t size, JSType type, uint32_t inlinedProps)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
2021-12-17 09:18:10 +00:00
|
|
|
ClearBitField();
|
2024-07-17 06:30:03 +00:00
|
|
|
if (IsJSTypeObject(type)) {
|
2021-12-17 09:18:10 +00:00
|
|
|
SetObjectSize(size + inlinedProps * JSTaggedValue::TaggedTypeSize());
|
|
|
|
SetInlinedPropsStart(size);
|
2021-09-04 08:06:49 +00:00
|
|
|
} else {
|
|
|
|
SetObjectSize(size);
|
|
|
|
}
|
2024-07-17 06:30:03 +00:00
|
|
|
SetLayout(thread, JSTaggedValue::Null());
|
2023-06-01 11:50:59 +00:00
|
|
|
if (type >= JSType::JS_FUNCTION_FIRST && type <= JSType::JS_FUNCTION_LAST) {
|
|
|
|
SetIsJSFunction(true);
|
|
|
|
}
|
2021-12-17 09:18:10 +00:00
|
|
|
SetPrototype(thread, JSTaggedValue::Null());
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
SetObjectType(type);
|
|
|
|
SetExtensible(true);
|
|
|
|
SetIsPrototype(false);
|
2022-11-24 07:55:30 +00:00
|
|
|
SetHasDeleteProperty(false);
|
2023-07-12 10:42:42 +00:00
|
|
|
SetIsAllTaggedProp(true);
|
2023-08-02 01:56:06 +00:00
|
|
|
SetElementsKind(ElementsKind::GENERIC);
|
2022-03-11 09:30:43 +00:00
|
|
|
SetTransitions(thread, JSTaggedValue::Undefined());
|
2023-08-21 12:11:01 +00:00
|
|
|
SetParent(thread, JSTaggedValue::Undefined());
|
2021-09-04 08:06:49 +00:00
|
|
|
SetProtoChangeMarker(thread, JSTaggedValue::Null());
|
|
|
|
SetProtoChangeDetails(thread, JSTaggedValue::Null());
|
|
|
|
SetEnumCache(thread, JSTaggedValue::Null());
|
2024-01-23 13:45:43 +00:00
|
|
|
}
|
|
|
|
|
2024-04-07 11:40:53 +00:00
|
|
|
bool JSHClass::IsJSTypeShared(JSType type)
|
2024-01-23 13:45:43 +00:00
|
|
|
{
|
2024-04-07 11:40:53 +00:00
|
|
|
bool isShared = false;
|
2024-02-28 01:53:23 +00:00
|
|
|
switch (type) {
|
|
|
|
case JSType::JS_SHARED_OBJECT:
|
|
|
|
case JSType::JS_SHARED_FUNCTION:
|
2024-07-07 08:14:44 +00:00
|
|
|
case JSType::JS_SHARED_ASYNC_FUNCTION:
|
2024-11-11 07:45:35 +00:00
|
|
|
case JSType::JS_API_BITVECTOR:
|
2024-02-28 01:53:23 +00:00
|
|
|
case JSType::JS_SHARED_SET:
|
|
|
|
case JSType::JS_SHARED_MAP:
|
2024-03-03 03:08:30 +00:00
|
|
|
case JSType::JS_SHARED_ARRAY:
|
2024-04-06 06:34:37 +00:00
|
|
|
case JSType::JS_SHARED_TYPED_ARRAY:
|
|
|
|
case JSType::JS_SHARED_INT8_ARRAY:
|
|
|
|
case JSType::JS_SHARED_UINT8_ARRAY:
|
|
|
|
case JSType::JS_SHARED_UINT8_CLAMPED_ARRAY:
|
|
|
|
case JSType::JS_SHARED_INT16_ARRAY:
|
|
|
|
case JSType::JS_SHARED_UINT16_ARRAY:
|
|
|
|
case JSType::JS_SHARED_INT32_ARRAY:
|
|
|
|
case JSType::JS_SHARED_UINT32_ARRAY:
|
|
|
|
case JSType::JS_SHARED_FLOAT32_ARRAY:
|
|
|
|
case JSType::JS_SHARED_FLOAT64_ARRAY:
|
|
|
|
case JSType::JS_SHARED_BIGINT64_ARRAY:
|
|
|
|
case JSType::JS_SHARED_BIGUINT64_ARRAY:
|
2024-04-07 14:06:42 +00:00
|
|
|
case JSType::JS_SENDABLE_ARRAY_BUFFER:
|
2024-03-22 17:09:45 +00:00
|
|
|
case JSType::BIGINT:
|
|
|
|
case JSType::LINE_STRING:
|
|
|
|
case JSType::CONSTANT_STRING:
|
|
|
|
case JSType::SLICED_STRING:
|
|
|
|
case JSType::TREE_STRING:
|
2024-04-07 11:40:53 +00:00
|
|
|
isShared = true;
|
2024-02-28 01:53:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2024-04-07 11:40:53 +00:00
|
|
|
return isShared;
|
|
|
|
}
|
|
|
|
|
2024-01-23 13:45:43 +00:00
|
|
|
// class JSHClass
|
|
|
|
void JSHClass::Initialize(const JSThread *thread, uint32_t size, JSType type, uint32_t inlinedProps)
|
|
|
|
{
|
|
|
|
InitializeWithDefaultValue(thread, size, type, inlinedProps);
|
2024-07-17 06:30:03 +00:00
|
|
|
if (IsJSTypeObject(type)) {
|
2024-04-07 15:11:25 +00:00
|
|
|
SetLayout(thread, thread->GlobalConstants()->GetEmptyLayoutInfo());
|
2024-01-23 13:45:43 +00:00
|
|
|
}
|
2023-03-08 02:22:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 13:45:43 +00:00
|
|
|
// for sharedHeap
|
|
|
|
void JSHClass::Initialize(const JSThread *thread, uint32_t size, JSType type,
|
|
|
|
uint32_t inlinedProps, const JSHandle<JSTaggedValue> &layout)
|
|
|
|
{
|
|
|
|
InitializeWithDefaultValue(thread, size, type, inlinedProps);
|
2024-07-17 06:30:03 +00:00
|
|
|
if (IsJSTypeObject(type)) {
|
2024-01-23 13:45:43 +00:00
|
|
|
SetLayout(thread, layout);
|
|
|
|
}
|
2024-04-07 11:40:53 +00:00
|
|
|
if (IsJSTypeShared(type)) {
|
|
|
|
SetIsJSShared(true);
|
2024-02-28 01:53:23 +00:00
|
|
|
}
|
2024-01-23 13:45:43 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 09:18:10 +00:00
|
|
|
JSHandle<JSHClass> JSHClass::Clone(const JSThread *thread, const JSHandle<JSHClass> &jshclass,
|
2023-10-31 06:47:29 +00:00
|
|
|
bool withoutInlinedProperties, uint32_t incInlinedProperties)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
JSType type = jshclass->GetObjectType();
|
2024-07-17 06:30:03 +00:00
|
|
|
uint32_t size = IsJSTypeObject(type) ? jshclass->GetInlinedPropsStartSize() : jshclass->GetObjectSize();
|
2023-10-31 06:47:29 +00:00
|
|
|
uint32_t numInlinedProps = withoutInlinedProperties ? 0 : jshclass->GetInlinedProperties() + incInlinedProperties;
|
2024-04-09 14:17:29 +00:00
|
|
|
JSHandle<JSHClass> newJsHClass;
|
|
|
|
if (jshclass.GetTaggedValue().IsInSharedHeap()) {
|
|
|
|
newJsHClass = thread->GetEcmaVM()->GetFactory()->NewSEcmaHClass(size, type, numInlinedProps);
|
|
|
|
} else {
|
|
|
|
newJsHClass = thread->GetEcmaVM()->GetFactory()->NewEcmaHClass(size, type, numInlinedProps);
|
|
|
|
}
|
2021-12-17 09:18:10 +00:00
|
|
|
// Copy all
|
2022-08-16 15:48:09 +00:00
|
|
|
newJsHClass->Copy(thread, *jshclass);
|
|
|
|
newJsHClass->SetTransitions(thread, JSTaggedValue::Undefined());
|
2023-08-21 12:11:01 +00:00
|
|
|
newJsHClass->SetParent(thread, JSTaggedValue::Undefined());
|
2022-08-16 15:48:09 +00:00
|
|
|
newJsHClass->SetProtoChangeDetails(thread, JSTaggedValue::Null());
|
|
|
|
newJsHClass->SetEnumCache(thread, JSTaggedValue::Null());
|
2021-09-04 08:06:49 +00:00
|
|
|
// reuse Attributes first.
|
2022-08-16 15:48:09 +00:00
|
|
|
newJsHClass->SetLayout(thread, jshclass->GetLayout());
|
2021-12-17 09:18:10 +00:00
|
|
|
|
2024-11-10 08:35:08 +00:00
|
|
|
if (jshclass->IsAOT()) {
|
|
|
|
newJsHClass->SetAOT(false);
|
2022-10-22 07:59:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 15:48:09 +00:00
|
|
|
return newJsHClass;
|
2021-12-17 09:18:10 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 08:21:36 +00:00
|
|
|
JSHandle<JSHClass> JSHClass::CloneWithElementsKind(const JSThread *thread, const JSHandle<JSHClass> &jshclass,
|
|
|
|
const ElementsKind kind, bool isPrototype)
|
|
|
|
{
|
|
|
|
JSHandle<JSHClass> newHClass = Clone(thread, jshclass);
|
|
|
|
newHClass->SetIsPrototype(isPrototype);
|
|
|
|
newHClass->SetElementsKind(kind);
|
|
|
|
return newHClass;
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:18:10 +00:00
|
|
|
// use for transition to dictionary
|
|
|
|
JSHandle<JSHClass> JSHClass::CloneWithoutInlinedProperties(const JSThread *thread, const JSHandle<JSHClass> &jshclass)
|
|
|
|
{
|
|
|
|
return Clone(thread, jshclass, true);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JSHClass::TransitionElementsToDictionary(const JSThread *thread, const JSHandle<JSObject> &obj)
|
|
|
|
{
|
|
|
|
// property transition to slow first
|
|
|
|
if (!obj->GetJSHClass()->IsDictionaryMode()) {
|
|
|
|
JSObject::TransitionToDictionary(thread, obj);
|
2024-08-09 08:48:24 +00:00
|
|
|
RETURN_IF_ABRUPT_COMPLETION(thread);
|
2023-11-09 08:29:23 +00:00
|
|
|
} else {
|
|
|
|
TransitionToDictionary(thread, obj);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
obj->GetJSHClass()->SetIsDictionaryElement(true);
|
2021-09-07 14:24:16 +00:00
|
|
|
obj->GetJSHClass()->SetIsStableElements(false);
|
2023-12-18 07:57:47 +00:00
|
|
|
obj->GetJSHClass()->SetElementsKind(ElementsKind::DICTIONARY);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 12:16:50 +00:00
|
|
|
void JSHClass::OptimizeAsFastElements(const JSThread *thread, JSHandle<JSObject> obj)
|
|
|
|
{
|
|
|
|
if (obj->GetJSHClass()->IsDictionaryMode()) {
|
|
|
|
JSObject::OptimizeAsFastProperties(thread, obj);
|
|
|
|
} else {
|
|
|
|
OptimizeAsFastProperties(thread, obj);
|
|
|
|
}
|
|
|
|
obj->GetJSHClass()->SetIsDictionaryElement(false);
|
|
|
|
obj->GetJSHClass()->SetIsStableElements(true);
|
|
|
|
obj->GetJSHClass()->SetElementsKind(ElementsKind::HOLE_TAGGED);
|
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
void JSHClass::AddProperty(const JSThread *thread, const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &key,
|
2024-07-09 12:34:51 +00:00
|
|
|
const PropertyAttributes &attr, const Representation &rep)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
JSHandle<JSHClass> jshclass(thread, obj->GetJSHClass());
|
2024-07-09 12:34:51 +00:00
|
|
|
auto metadata = JSTaggedValue(attr.GetPropertyMetaData());
|
|
|
|
JSHClass *newClass = jshclass->FindTransitions(key.GetTaggedValue(), metadata, rep);
|
2022-08-16 15:48:09 +00:00
|
|
|
if (newClass != nullptr) {
|
2023-12-22 05:41:23 +00:00
|
|
|
// The transition hclass from AOT, which does not have a prototype, needs to be reset here.
|
2024-11-10 08:35:08 +00:00
|
|
|
if (newClass->IsAOT()) {
|
2023-12-22 05:41:23 +00:00
|
|
|
newClass->SetPrototype(thread, jshclass->GetPrototype());
|
|
|
|
}
|
2023-12-18 07:57:47 +00:00
|
|
|
// Because we currently only supports Fast ElementsKind
|
2024-08-06 13:33:05 +00:00
|
|
|
RestoreElementsKindToGeneric(newClass);
|
|
|
|
obj->SynchronizedSetClass(thread, newClass);
|
2021-09-07 14:24:16 +00:00
|
|
|
#if ECMASCRIPT_ENABLE_IC
|
2023-12-22 05:41:23 +00:00
|
|
|
// The transition hclass from AOT, which does not have protochangemarker, needs to be reset here
|
2024-08-06 13:33:05 +00:00
|
|
|
JSHandle<JSHClass> newHClass = JSHandle<JSHClass>(thread, newClass);
|
2024-11-10 08:35:08 +00:00
|
|
|
if (newClass->IsAOT() && newClass->IsPrototype()) {
|
2024-07-31 02:41:31 +00:00
|
|
|
if (JSHClass::IsNeedNotifyHclassChangedForAotTransition(thread, jshclass, key.GetTaggedValue())) {
|
2024-08-06 13:33:05 +00:00
|
|
|
JSHClass::EnableProtoChangeMarker(thread, newHClass);
|
2024-07-31 02:41:31 +00:00
|
|
|
JSHClass::NotifyHclassChanged(thread, jshclass, newHClass, key.GetTaggedValue());
|
|
|
|
} else {
|
|
|
|
JSHClass::RefreshUsers(thread, jshclass, newHClass);
|
|
|
|
}
|
|
|
|
JSHClass::EnablePHCProtoChangeMarker(thread, newHClass);
|
|
|
|
} else {
|
|
|
|
JSHClass::NotifyHclassChanged(thread, jshclass, newHClass, key.GetTaggedValue());
|
2023-12-22 05:41:23 +00:00
|
|
|
}
|
2024-07-31 02:41:31 +00:00
|
|
|
#endif
|
2021-09-04 08:06:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-16 15:48:09 +00:00
|
|
|
JSHandle<JSHClass> newJsHClass = JSHClass::Clone(thread, jshclass);
|
2024-08-06 13:33:05 +00:00
|
|
|
RestoreElementsKindToGeneric(*newJsHClass);
|
2023-12-13 02:09:11 +00:00
|
|
|
AddPropertyToNewHClass(thread, jshclass, newJsHClass, key, attr);
|
|
|
|
// update hclass in object.
|
2021-09-07 14:24:16 +00:00
|
|
|
#if ECMASCRIPT_ENABLE_IC
|
2023-05-23 03:37:28 +00:00
|
|
|
JSHClass::NotifyHclassChanged(thread, jshclass, newJsHClass, key.GetTaggedValue());
|
2021-09-04 08:06:49 +00:00
|
|
|
#endif
|
2023-12-18 07:57:47 +00:00
|
|
|
// Because we currently only supports Fast ElementsKind
|
2024-08-06 13:33:05 +00:00
|
|
|
obj->SynchronizedSetClass(thread, *newJsHClass);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSHandle<JSHClass> JSHClass::TransitionExtension(const JSThread *thread, const JSHandle<JSHClass> &jshclass)
|
|
|
|
{
|
|
|
|
JSHandle<JSTaggedValue> key(thread->GlobalConstants()->GetHandledPreventExtensionsString());
|
|
|
|
{
|
2024-07-09 12:34:51 +00:00
|
|
|
auto *newClass = jshclass->FindTransitions(key.GetTaggedValue(), JSTaggedValue(0), Representation::NONE);
|
2022-08-16 15:48:09 +00:00
|
|
|
if (newClass != nullptr) {
|
2023-10-31 06:47:29 +00:00
|
|
|
newClass->SetPrototype(thread, jshclass->GetPrototype());
|
2022-08-16 15:48:09 +00:00
|
|
|
return JSHandle<JSHClass>(thread, newClass);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
|
|
// 2. new a hclass
|
2022-08-16 15:48:09 +00:00
|
|
|
JSHandle<JSHClass> newJsHClass = JSHClass::Clone(thread, jshclass);
|
|
|
|
newJsHClass->SetExtensible(false);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-08-16 15:48:09 +00:00
|
|
|
JSTaggedValue attrs = newJsHClass->GetLayout();
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
JSMutableHandle<LayoutInfo> layoutInfoHandle(thread, attrs);
|
|
|
|
layoutInfoHandle.Update(factory->CopyLayoutInfo(layoutInfoHandle).GetTaggedValue());
|
2022-08-16 15:48:09 +00:00
|
|
|
newJsHClass->SetLayout(thread, layoutInfoHandle);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 15:48:09 +00:00
|
|
|
// 3. Add newClass to old hclass's parent's transitions.
|
|
|
|
AddExtensionTransitions(thread, jshclass, newJsHClass, key);
|
2021-09-04 08:06:49 +00:00
|
|
|
// parent is the same as jshclass, already copy
|
2022-08-16 15:48:09 +00:00
|
|
|
return newJsHClass;
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSHandle<JSHClass> JSHClass::TransitionProto(const JSThread *thread, const JSHandle<JSHClass> &jshclass,
|
2024-09-02 07:04:39 +00:00
|
|
|
const JSHandle<JSTaggedValue> &proto, bool isChangeProto)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
JSHandle<JSTaggedValue> key(thread->GlobalConstants()->GetHandledPrototypeString());
|
|
|
|
|
|
|
|
{
|
2022-08-16 15:48:09 +00:00
|
|
|
auto *newClass = jshclass->FindProtoTransitions(key.GetTaggedValue(), proto.GetTaggedValue());
|
|
|
|
if (newClass != nullptr) {
|
|
|
|
return JSHandle<JSHClass>(thread, newClass);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
2024-07-05 00:59:49 +00:00
|
|
|
newClass = FindTransitionProtoForAOT(thread, jshclass, proto);
|
|
|
|
if (newClass != nullptr) {
|
|
|
|
return JSHandle<JSHClass>(thread, newClass);
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
|
|
// 2. new a hclass
|
2022-08-16 15:48:09 +00:00
|
|
|
JSHandle<JSHClass> newJsHClass = JSHClass::Clone(thread, jshclass);
|
2024-09-02 07:04:39 +00:00
|
|
|
newJsHClass->SetPrototype(thread, proto.GetTaggedValue(), isChangeProto);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-08-16 15:48:09 +00:00
|
|
|
JSTaggedValue layout = newJsHClass->GetLayout();
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2022-03-11 09:30:43 +00:00
|
|
|
JSMutableHandle<LayoutInfo> layoutInfoHandle(thread, layout);
|
2021-09-04 08:06:49 +00:00
|
|
|
layoutInfoHandle.Update(factory->CopyLayoutInfo(layoutInfoHandle).GetTaggedValue());
|
2022-08-16 15:48:09 +00:00
|
|
|
newJsHClass->SetLayout(thread, layoutInfoHandle);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 15:48:09 +00:00
|
|
|
// 3. Add newJsHClass to old jshclass's parent's transitions.
|
|
|
|
AddProtoTransitions(thread, jshclass, newJsHClass, key, proto);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
// parent is the same as jshclass, already copy
|
2022-08-16 15:48:09 +00:00
|
|
|
return newJsHClass;
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 00:59:49 +00:00
|
|
|
// static
|
|
|
|
JSHClass *JSHClass::FindTransitionProtoForAOT(const JSThread *thread, const JSHandle<JSHClass> &jshclass,
|
|
|
|
const JSHandle<JSTaggedValue> &proto)
|
|
|
|
{
|
|
|
|
if (!proto->IsECMAObject()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
JSHandle<JSHClass> baseIhc(thread, proto->GetTaggedObject()->GetClass());
|
2024-11-10 08:35:08 +00:00
|
|
|
if (!jshclass->IsAOT() || !baseIhc->IsAOT()) {
|
2024-07-05 00:59:49 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto transitionTable = thread->GetCurrentEcmaContext()->GetFunctionProtoTransitionTable();
|
|
|
|
auto transHc = transitionTable->FindTransitionByHClass(thread,
|
|
|
|
JSHandle<JSTaggedValue>(jshclass),
|
|
|
|
JSHandle<JSTaggedValue>(baseIhc));
|
|
|
|
JSHandle<JSTaggedValue> transIhc(thread, transHc.first);
|
|
|
|
JSHandle<JSTaggedValue> transPhc(thread, transHc.second);
|
|
|
|
if (transIhc->IsUndefined() || transPhc->IsUndefined()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ReBuildFunctionInheritanceRelationship(thread, proto, JSHandle<JSTaggedValue>(baseIhc), transIhc, transPhc);
|
|
|
|
return JSHClass::Cast(transIhc->GetTaggedObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void JSHClass::ReBuildFunctionInheritanceRelationship(const JSThread *thread,
|
|
|
|
const JSHandle<JSTaggedValue> &proto,
|
|
|
|
const JSHandle<JSTaggedValue> &baseIhc,
|
|
|
|
const JSHandle<JSTaggedValue> &transIhc,
|
|
|
|
const JSHandle<JSTaggedValue> &transPhc)
|
|
|
|
{
|
|
|
|
JSHandle<JSHClass>::Cast(transIhc)->SetProto(thread, proto.GetTaggedValue());
|
|
|
|
if (baseIhc.GetTaggedType() == transPhc.GetTaggedType()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// use transPhc to replace the hclass of proto
|
|
|
|
JSHandle<JSHClass> oldPhc(thread, proto->GetTaggedObject()->GetClass());
|
|
|
|
proto->GetTaggedObject()->SynchronizedSetClass(thread, JSHClass::Cast(transPhc->GetTaggedObject()));
|
|
|
|
ASSERT(JSHClass::Cast(transPhc->GetTaggedObject())->IsPrototype());
|
|
|
|
// update the prototype of new phc
|
|
|
|
JSHClass::Cast(transPhc->GetTaggedObject())->SetPrototype(thread, oldPhc->GetPrototype());
|
|
|
|
// enable prototype change marker
|
|
|
|
JSTaggedValue phcPrototype = JSHClass::Cast(transPhc->GetTaggedObject())->GetPrototype();
|
|
|
|
JSHandle<JSTaggedValue> parentPrototype(thread, phcPrototype);
|
|
|
|
ASSERT(parentPrototype->IsECMAObject());
|
|
|
|
JSHClass::EnablePHCProtoChangeMarker(thread,
|
|
|
|
JSHandle<JSHClass>(thread, parentPrototype->GetTaggedObject()->GetClass()));
|
|
|
|
JSHClass::EnableProtoChangeMarker(thread, JSHandle<JSHClass>(transIhc));
|
|
|
|
}
|
|
|
|
|
2023-09-27 07:11:36 +00:00
|
|
|
JSHandle<JSHClass> JSHClass::CloneWithAddProto(const JSThread *thread, const JSHandle<JSHClass> &jshclass,
|
|
|
|
const JSHandle<JSTaggedValue> &key,
|
|
|
|
const JSHandle<JSTaggedValue> &proto)
|
|
|
|
{
|
|
|
|
// 1. new a hclass
|
|
|
|
JSHandle<JSHClass> newJsHClass = JSHClass::Clone(thread, jshclass);
|
|
|
|
newJsHClass->SetPrototype(thread, proto.GetTaggedValue());
|
|
|
|
|
|
|
|
// 2. Add newJsHClass to old jshclass's parent's transitions.
|
|
|
|
AddProtoTransitions(thread, jshclass, newJsHClass, key, proto);
|
|
|
|
// parent is the same as jshclass, already copy
|
|
|
|
return newJsHClass;
|
|
|
|
}
|
|
|
|
|
2022-06-13 14:05:01 +00:00
|
|
|
JSHandle<JSHClass> JSHClass::TransProtoWithoutLayout(const JSThread *thread, const JSHandle<JSHClass> &jshclass,
|
|
|
|
const JSHandle<JSTaggedValue> &proto)
|
|
|
|
{
|
|
|
|
JSHandle<JSTaggedValue> key(thread->GlobalConstants()->GetHandledPrototypeString());
|
|
|
|
|
|
|
|
{
|
2022-08-16 15:48:09 +00:00
|
|
|
auto *newClass = jshclass->FindProtoTransitions(key.GetTaggedValue(), proto.GetTaggedValue());
|
|
|
|
if (newClass != nullptr) {
|
|
|
|
return JSHandle<JSHClass>(thread, newClass);
|
2022-06-13 14:05:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-27 07:11:36 +00:00
|
|
|
return CloneWithAddProto(thread, jshclass, key, proto);
|
2022-06-13 14:05:01 +00:00
|
|
|
}
|
|
|
|
|
2024-09-02 07:04:39 +00:00
|
|
|
void JSHClass::SetPrototype(const JSThread *thread, JSTaggedValue proto, bool isChangeProto)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2024-02-03 15:55:42 +00:00
|
|
|
// Because the heap-space of hclass is non-movable, this function can be non-static.
|
2023-03-30 08:27:04 +00:00
|
|
|
JSHandle<JSTaggedValue> protoHandle(thread, proto);
|
2024-09-02 07:04:39 +00:00
|
|
|
SetPrototype(thread, protoHandle, isChangeProto);
|
2023-03-30 08:27:04 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 16:19:52 +00:00
|
|
|
JSHandle<JSHClass> JSHClass::SetPrototypeWithNotification(const JSThread *thread,
|
|
|
|
const JSHandle<JSHClass> &hclass,
|
2024-09-02 07:04:39 +00:00
|
|
|
const JSHandle<JSTaggedValue> &proto,
|
|
|
|
bool isChangeProto)
|
2024-02-03 15:55:42 +00:00
|
|
|
{
|
2024-07-30 16:19:52 +00:00
|
|
|
// `hclass` can become prototype inside `TransitionProto` if `hclass` is HClass of `proto`.
|
|
|
|
// In this case we don't need to notify
|
|
|
|
auto wasPrototype = hclass->IsPrototype();
|
2024-09-02 07:04:39 +00:00
|
|
|
JSHandle<JSHClass> newClass = JSHClass::TransitionProto(thread, hclass, proto, isChangeProto);
|
2024-07-30 16:19:52 +00:00
|
|
|
if (wasPrototype) {
|
|
|
|
ASSERT(hclass->IsPrototype());
|
|
|
|
JSHClass::NotifyHclassChanged(thread, hclass, newClass);
|
|
|
|
}
|
|
|
|
return newClass;
|
2024-02-03 15:55:42 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 09:15:56 +00:00
|
|
|
void JSHClass::SetPrototypeTransition(JSThread *thread, const JSHandle<JSObject> &object,
|
2024-09-02 07:04:39 +00:00
|
|
|
const JSHandle<JSTaggedValue> &proto, bool isChangeProto)
|
2024-05-18 09:15:56 +00:00
|
|
|
{
|
|
|
|
JSHandle<JSHClass> hclass(thread, object->GetJSHClass());
|
2024-09-02 07:04:39 +00:00
|
|
|
auto newClass = SetPrototypeWithNotification(thread, hclass, proto, isChangeProto);
|
2024-08-06 13:33:05 +00:00
|
|
|
RestoreElementsKindToGeneric(*newClass);
|
2024-05-18 09:15:56 +00:00
|
|
|
object->SynchronizedSetClass(thread, *newClass);
|
2024-11-12 08:30:29 +00:00
|
|
|
if (object->IsJSArray()) {
|
|
|
|
thread->NotifyArrayPrototypeChangedGuardians(object);
|
|
|
|
newClass->SetIsJSArrayPrototypeModified(true);
|
|
|
|
}
|
2024-05-18 09:15:56 +00:00
|
|
|
ObjectOperator::UpdateDetectorOnSetPrototype(thread, object.GetTaggedValue());
|
|
|
|
}
|
|
|
|
|
2024-09-02 07:04:39 +00:00
|
|
|
void JSHClass::SetPrototype(const JSThread *thread, const JSHandle<JSTaggedValue> &proto, bool isChangeProto)
|
2023-03-30 08:27:04 +00:00
|
|
|
{
|
2024-02-03 15:55:42 +00:00
|
|
|
// Because the heap-space of hclass is non-movable, this function can be non-static.
|
2023-03-30 08:27:04 +00:00
|
|
|
if (proto->IsJSObject()) {
|
2024-09-02 07:04:39 +00:00
|
|
|
OptimizePrototypeForIC(thread, proto, isChangeProto);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
SetProto(thread, proto);
|
|
|
|
}
|
2021-12-25 01:51:43 +00:00
|
|
|
|
2024-07-05 00:59:49 +00:00
|
|
|
void JSHClass::OptimizePrototypeForIC(const JSThread *thread, const JSHandle<JSTaggedValue> &proto, bool isChangeProto)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2023-03-30 08:27:04 +00:00
|
|
|
JSHandle<JSHClass> hclass(thread, proto->GetTaggedObject()->GetClass());
|
|
|
|
ASSERT(!Region::ObjectAddressToRange(reinterpret_cast<TaggedObject *>(*hclass))->InReadOnlySpace());
|
|
|
|
if (!hclass->IsPrototype()) {
|
2024-07-05 00:59:49 +00:00
|
|
|
// Situations for clone proto hclass:
|
|
|
|
// 1: unshared non-ts hclass
|
|
|
|
// 2: no matter whether hclass is ts or not when set function prototype
|
2024-11-10 08:35:08 +00:00
|
|
|
if ((!hclass->IsAOT() && !hclass->IsJSShared()) || isChangeProto) {
|
2024-02-03 15:55:42 +00:00
|
|
|
// The local IC and on-proto IC are different, because the former don't need to notify the whole
|
|
|
|
// prototype-chain or listen the changes of prototype chain, but the latter do. Therefore, when
|
|
|
|
// an object becomes a prototype object at the first time, we need to copy its hidden class in
|
|
|
|
// order to maintain the previously generated local IC and support the on-proto IC in the future.
|
|
|
|
// For example, a local IC adds a new property x for o1 and the o1.hclass1 -> o1.hclass2, when the
|
|
|
|
// o1 becomes a prototype object of object o2 and an on-proto IC loading x from o2 will rely on the
|
|
|
|
// stability of the prototype-chain o2 -> o1. If directly marking the o1.hclass1 as a prototype hclass,
|
|
|
|
// the previous IC of adding property x won't trigger IC-miss and fails to notify the IC on o2.
|
2024-07-20 08:21:36 +00:00
|
|
|
|
|
|
|
// At here, When a JSArray with initial hclass is set as a proto,
|
|
|
|
// we substitute its hclass with preserved proto hclass.
|
|
|
|
JSHandle<JSHClass> newProtoClass;
|
|
|
|
if (ProtoIsFastJSArray(thread, proto, hclass)) {
|
|
|
|
newProtoClass = JSHandle<JSHClass>(thread, thread->GetArrayInstanceHClass(hclass->GetElementsKind(),
|
|
|
|
true));
|
|
|
|
} else {
|
|
|
|
newProtoClass = JSHClass::Clone(thread, hclass);
|
|
|
|
}
|
2023-11-09 02:58:18 +00:00
|
|
|
JSTaggedValue layout = newProtoClass->GetLayout();
|
|
|
|
// If the type of object is JSObject, the layout info value is initialized to the default value,
|
|
|
|
// if the value is not JSObject, the layout info value is initialized to null.
|
|
|
|
if (!layout.IsNull()) {
|
|
|
|
JSMutableHandle<LayoutInfo> layoutInfoHandle(thread, layout);
|
|
|
|
layoutInfoHandle.Update(
|
|
|
|
thread->GetEcmaVM()->GetFactory()->CopyLayoutInfo(layoutInfoHandle).GetTaggedValue());
|
|
|
|
newProtoClass->SetLayout(thread, layoutInfoHandle);
|
|
|
|
}
|
2023-03-30 08:27:04 +00:00
|
|
|
|
|
|
|
#if ECMASCRIPT_ENABLE_IC
|
2023-11-09 02:58:18 +00:00
|
|
|
// After the hclass is updated, check whether the proto chain status of ic is updated.
|
|
|
|
NotifyHclassChanged(thread, hclass, newProtoClass);
|
2023-03-30 08:27:04 +00:00
|
|
|
#endif
|
2024-01-11 12:01:51 +00:00
|
|
|
JSObject::Cast(proto->GetTaggedObject())->SynchronizedSetClass(thread, *newProtoClass);
|
2023-11-09 02:58:18 +00:00
|
|
|
newProtoClass->SetIsPrototype(true);
|
2024-07-05 00:59:49 +00:00
|
|
|
// still dump for class in this path now
|
|
|
|
if (!isChangeProto) {
|
|
|
|
thread->GetEcmaVM()->GetPGOProfiler()->UpdateRootProfileTypeSafe(*hclass, *newProtoClass);
|
|
|
|
}
|
2023-11-09 02:58:18 +00:00
|
|
|
} else {
|
2024-02-03 15:55:42 +00:00
|
|
|
// There is no sharing in AOT hclass. Therefore, it is not necessary or possible to clone here.
|
2023-11-09 02:58:18 +00:00
|
|
|
hclass->SetIsPrototype(true);
|
|
|
|
}
|
2023-03-30 08:27:04 +00:00
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JSHClass::TransitionToDictionary(const JSThread *thread, const JSHandle<JSObject> &obj)
|
|
|
|
{
|
|
|
|
// 1. new a hclass
|
2021-12-17 09:18:10 +00:00
|
|
|
JSHandle<JSHClass> jshclass(thread, obj->GetJSHClass());
|
2022-08-16 15:48:09 +00:00
|
|
|
JSHandle<JSHClass> newJsHClass = CloneWithoutInlinedProperties(thread, jshclass);
|
2021-12-17 09:18:10 +00:00
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
// 2. Copy
|
2022-08-16 15:48:09 +00:00
|
|
|
newJsHClass->SetNumberOfProps(0);
|
|
|
|
newJsHClass->SetIsDictionaryMode(true);
|
|
|
|
ASSERT(newJsHClass->GetInlinedProperties() == 0);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-08-16 15:48:09 +00:00
|
|
|
// 3. Add newJsHClass to ?
|
2021-09-07 14:24:16 +00:00
|
|
|
#if ECMASCRIPT_ENABLE_IC
|
2022-08-16 15:48:09 +00:00
|
|
|
JSHClass::NotifyHclassChanged(thread, JSHandle<JSHClass>(thread, obj->GetJSHClass()), newJsHClass);
|
2021-09-04 08:06:49 +00:00
|
|
|
#endif
|
2024-08-06 13:33:05 +00:00
|
|
|
RestoreElementsKindToGeneric(*newJsHClass);
|
2024-01-11 12:01:51 +00:00
|
|
|
obj->SynchronizedSetClass(thread, *newJsHClass);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 12:16:50 +00:00
|
|
|
void JSHClass::OptimizeAsFastProperties(const JSThread *thread, const JSHandle<JSObject> &obj,
|
|
|
|
const std::vector<int> &indexOrder, bool isDictionary)
|
|
|
|
{
|
|
|
|
// 1. new a hclass
|
|
|
|
JSHandle<JSHClass> jshclass(thread, obj->GetJSHClass());
|
|
|
|
JSHandle<JSHClass> newJsHClass = Clone(thread, jshclass, isDictionary);
|
|
|
|
|
|
|
|
// 2. If it is dictionary, migrate should change layout. otherwise, copy the hclass only.
|
|
|
|
if (isDictionary) {
|
2024-07-18 10:42:22 +00:00
|
|
|
JSHandle<NameDictionary> properties(thread, obj->GetProperties());
|
|
|
|
int numberOfProperties = properties->EntriesCount();
|
2023-10-30 12:16:50 +00:00
|
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
|
|
JSHandle<LayoutInfo> layoutInfoHandle = factory->CreateLayoutInfo(numberOfProperties);
|
2024-03-28 06:08:28 +00:00
|
|
|
int numberOfInlinedProps = static_cast<int>(newJsHClass->GetInlinedProperties());
|
2023-10-30 12:16:50 +00:00
|
|
|
for (int i = 0; i < numberOfProperties; i++) {
|
|
|
|
JSTaggedValue key = properties->GetKey(indexOrder[i]);
|
|
|
|
PropertyAttributes attributes = properties->GetAttributes(indexOrder[i]);
|
|
|
|
if (i < numberOfInlinedProps) {
|
|
|
|
attributes.SetIsInlinedProps(true);
|
|
|
|
} else {
|
|
|
|
attributes.SetIsInlinedProps(false);
|
|
|
|
}
|
|
|
|
attributes.SetOffset(i);
|
|
|
|
layoutInfoHandle->AddKey(thread, i, key, attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
newJsHClass->SetNumberOfProps(numberOfProperties);
|
|
|
|
newJsHClass->SetLayout(thread, layoutInfoHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
// 3. Copy
|
|
|
|
newJsHClass->SetIsDictionaryMode(false);
|
|
|
|
// 4. Add newJsHClass to ?
|
|
|
|
#if ECMASCRIPT_ENABLE_IC
|
|
|
|
JSHClass::NotifyHclassChanged(thread, JSHandle<JSHClass>(thread, obj->GetJSHClass()), newJsHClass);
|
|
|
|
#endif
|
2024-01-11 12:01:51 +00:00
|
|
|
obj->SynchronizedSetClass(thread, *newJsHClass);
|
2023-10-30 12:16:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 10:42:42 +00:00
|
|
|
void JSHClass::TransitionForRepChange(const JSThread *thread, const JSHandle<JSObject> &receiver,
|
|
|
|
const JSHandle<JSTaggedValue> &key, PropertyAttributes attr)
|
|
|
|
{
|
|
|
|
JSHandle<JSHClass> oldHClass(thread, receiver->GetJSHClass());
|
|
|
|
|
|
|
|
// 1. Create hclass and copy layout
|
|
|
|
JSHandle<JSHClass> newHClass = JSHClass::Clone(thread, oldHClass);
|
2024-08-06 13:33:05 +00:00
|
|
|
RestoreElementsKindToGeneric(*newHClass);
|
2023-07-12 10:42:42 +00:00
|
|
|
|
|
|
|
JSHandle<LayoutInfo> oldLayout(thread, newHClass->GetLayout());
|
|
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
|
|
JSHandle<LayoutInfo> newLayout(factory->CopyLayoutInfo(oldLayout));
|
|
|
|
newHClass->SetLayout(thread, newLayout);
|
|
|
|
|
|
|
|
// 2. update attr
|
|
|
|
auto hclass = JSHClass::Cast(newHClass.GetTaggedValue().GetTaggedObject());
|
|
|
|
int entry = JSHClass::FindPropertyEntry(thread, hclass, key.GetTaggedValue());
|
|
|
|
ASSERT(entry != -1);
|
|
|
|
newLayout->SetNormalAttr(thread, entry, attr);
|
|
|
|
|
|
|
|
// 3. update hclass in object.
|
|
|
|
#if ECMASCRIPT_ENABLE_IC
|
|
|
|
JSHClass::NotifyHclassChanged(thread, oldHClass, newHClass, key.GetTaggedValue());
|
|
|
|
#endif
|
|
|
|
|
2024-01-11 12:01:51 +00:00
|
|
|
receiver->SynchronizedSetClass(thread, *newHClass);
|
2023-07-12 10:42:42 +00:00
|
|
|
// 4. Maybe Transition And Maintain subtypeing check
|
|
|
|
}
|
|
|
|
|
2024-07-20 08:21:36 +00:00
|
|
|
bool JSHClass::IsInitialArrayHClassWithElementsKind(const JSThread *thread, const JSHClass *targetHClass,
|
|
|
|
const ElementsKind targetKind)
|
2023-12-18 07:57:47 +00:00
|
|
|
{
|
|
|
|
const auto &arrayHClassIndexMap = thread->GetArrayHClassIndexMap();
|
2024-07-20 08:21:36 +00:00
|
|
|
auto newKindIter = arrayHClassIndexMap.find(targetKind);
|
2023-12-18 07:57:47 +00:00
|
|
|
if (newKindIter != arrayHClassIndexMap.end()) {
|
2024-07-20 08:21:36 +00:00
|
|
|
auto indexPair = newKindIter->second;
|
|
|
|
auto hclassVal = thread->GlobalConstants()->GetGlobalConstantObject(static_cast<size_t>(indexPair.first));
|
|
|
|
auto hclassWithProtoVal = thread->GlobalConstants()->
|
|
|
|
GetGlobalConstantObject(static_cast<size_t>(indexPair.second));
|
2023-12-18 07:57:47 +00:00
|
|
|
JSHClass *hclass = JSHClass::Cast(hclassVal.GetTaggedObject());
|
2024-07-20 08:21:36 +00:00
|
|
|
JSHClass *hclassWithProto = JSHClass::Cast(hclassWithProtoVal.GetTaggedObject());
|
|
|
|
return (targetHClass == hclass || targetHClass == hclassWithProto);
|
2023-12-18 07:57:47 +00:00
|
|
|
}
|
2024-07-20 08:21:36 +00:00
|
|
|
return false;
|
2023-12-18 07:57:47 +00:00
|
|
|
}
|
|
|
|
|
2024-02-19 06:55:36 +00:00
|
|
|
bool JSHClass::TransitToElementsKindUncheck(const JSThread *thread, const JSHandle<JSObject> &obj,
|
|
|
|
ElementsKind newKind)
|
|
|
|
{
|
|
|
|
ElementsKind current = obj->GetJSHClass()->GetElementsKind();
|
|
|
|
// currently we only support initial array hclass
|
2024-07-20 08:21:36 +00:00
|
|
|
JSHClass *objHclass = obj->GetClass();
|
|
|
|
if (IsInitialArrayHClassWithElementsKind(thread, objHclass, current)) {
|
2024-02-19 06:55:36 +00:00
|
|
|
const auto &arrayHClassIndexMap = thread->GetArrayHClassIndexMap();
|
|
|
|
auto newKindIter = arrayHClassIndexMap.find(newKind);
|
2024-07-20 08:21:36 +00:00
|
|
|
bool objHclassIsPrototype = objHclass->IsPrototype();
|
2024-02-19 06:55:36 +00:00
|
|
|
if (newKindIter != arrayHClassIndexMap.end()) {
|
2024-07-20 08:21:36 +00:00
|
|
|
auto indexPair = newKindIter->second;
|
|
|
|
auto index = objHclassIsPrototype ? static_cast<size_t>(indexPair.second) :
|
|
|
|
static_cast<size_t>(indexPair.first);
|
2024-02-19 06:55:36 +00:00
|
|
|
auto hclassVal = thread->GlobalConstants()->GetGlobalConstantObject(index);
|
|
|
|
JSHClass *hclass = JSHClass::Cast(hclassVal.GetTaggedObject());
|
|
|
|
obj->SynchronizedSetClass(thread, hclass);
|
2024-10-17 09:25:55 +00:00
|
|
|
#if ECMASCRIPT_ENABLE_IC
|
|
|
|
JSHClass::NotifyHclassChanged(thread, JSHandle<JSHClass>(thread, objHclass),
|
|
|
|
JSHandle<JSHClass>(thread, hclass));
|
|
|
|
#endif
|
2024-02-19 06:55:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2024-07-20 08:21:36 +00:00
|
|
|
LOG_ECMA(FATAL) << "Unknown newKind: " << static_cast<int32_t>(newKind);
|
2024-02-19 06:55:36 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-18 07:57:47 +00:00
|
|
|
void JSHClass::TransitToElementsKind(const JSThread *thread, const JSHandle<JSArray> &array,
|
|
|
|
ElementsKind newKind)
|
2023-08-02 01:56:06 +00:00
|
|
|
{
|
|
|
|
JSTaggedValue elements = array->GetElements();
|
|
|
|
if (!elements.IsTaggedArray()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ElementsKind current = array->GetJSHClass()->GetElementsKind();
|
2023-12-18 07:57:47 +00:00
|
|
|
newKind = Elements::MergeElementsKind(newKind, current);
|
2023-08-02 01:56:06 +00:00
|
|
|
if (newKind == current) {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-20 08:21:36 +00:00
|
|
|
|
|
|
|
ASSERT(IsInitialArrayHClassWithElementsKind(thread, array->GetJSHClass(), current));
|
|
|
|
TransitToElementsKindUncheck(thread, JSHandle<JSObject>(array), newKind);
|
2023-08-02 01:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 07:57:47 +00:00
|
|
|
bool JSHClass::TransitToElementsKind(const JSThread *thread, const JSHandle<JSObject> &object,
|
|
|
|
const JSHandle<JSTaggedValue> &value, ElementsKind kind)
|
2023-08-02 01:56:06 +00:00
|
|
|
{
|
|
|
|
if (!object->IsJSArray()) {
|
2023-08-21 12:11:01 +00:00
|
|
|
return false;
|
2023-08-02 01:56:06 +00:00
|
|
|
}
|
|
|
|
ElementsKind current = object->GetJSHClass()->GetElementsKind();
|
|
|
|
if (Elements::IsGeneric(current)) {
|
2023-08-21 12:11:01 +00:00
|
|
|
return false;
|
2023-08-02 01:56:06 +00:00
|
|
|
}
|
|
|
|
auto newKind = Elements::ToElementsKind(value.GetTaggedValue(), kind);
|
|
|
|
// Merge current kind and new kind
|
|
|
|
newKind = Elements::MergeElementsKind(current, newKind);
|
|
|
|
if (newKind == current) {
|
2023-08-21 12:11:01 +00:00
|
|
|
return false;
|
2023-08-02 01:56:06 +00:00
|
|
|
}
|
2023-12-18 07:57:47 +00:00
|
|
|
// Currently, we only support fast array elementsKind
|
2024-07-20 08:21:36 +00:00
|
|
|
ASSERT(IsInitialArrayHClassWithElementsKind(thread, object->GetJSHClass(), current));
|
|
|
|
if (!TransitToElementsKindUncheck(thread, object, newKind)) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-19 06:55:36 +00:00
|
|
|
|
2024-07-20 08:21:36 +00:00
|
|
|
if (!thread->GetEcmaVM()->IsEnableElementsKind()) {
|
|
|
|
// Update TrackInfo
|
|
|
|
if (!thread->IsPGOProfilerEnable()) {
|
2023-08-21 12:11:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2024-07-20 08:21:36 +00:00
|
|
|
auto trackInfoVal = JSHandle<JSArray>(object)->GetTrackInfo();
|
|
|
|
thread->GetEcmaVM()->GetPGOProfiler()->UpdateTrackElementsKind(trackInfoVal, newKind);
|
2023-08-21 12:11:01 +00:00
|
|
|
return true;
|
2023-08-02 01:56:06 +00:00
|
|
|
}
|
2024-07-20 08:21:36 +00:00
|
|
|
return true;
|
2023-08-02 01:56:06 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 13:06:12 +00:00
|
|
|
void JSHClass::UpdateFieldType(JSHClass *hclass, const PropertyAttributes &attr)
|
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
JSHClass *ownHClass = FindFieldOwnHClass(hclass, attr);
|
|
|
|
VisitAndUpdateLayout(ownHClass, attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSHClass *JSHClass::FindFieldOwnHClass(JSHClass *hclass, const PropertyAttributes &attr)
|
|
|
|
{
|
|
|
|
uint32_t offset = attr.GetOffset();
|
|
|
|
JSTaggedValue parent(hclass);
|
|
|
|
JSHClass *curHClass = hclass;
|
|
|
|
while (parent.IsJSHClass()) {
|
|
|
|
auto parentHClass = JSHClass::Cast(parent.GetTaggedObject());
|
|
|
|
if (parentHClass->NumberOfProps() <= offset) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curHClass = parentHClass;
|
|
|
|
parent = curHClass->GetParent();
|
|
|
|
}
|
|
|
|
return curHClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSHClass::VisitAndUpdateLayout(JSHClass *ownHClass, const PropertyAttributes &attr)
|
|
|
|
{
|
|
|
|
uint32_t offset = attr.GetOffset();
|
|
|
|
auto targetTrackType = attr.GetTrackType();
|
|
|
|
std::queue<JSHClass *> backHClass;
|
|
|
|
backHClass.push(ownHClass);
|
|
|
|
while (!backHClass.empty()) {
|
|
|
|
JSHClass *current = backHClass.front();
|
|
|
|
backHClass.pop();
|
|
|
|
|
|
|
|
auto layout = LayoutInfo::Cast(current->GetLayout().GetTaggedObject());
|
|
|
|
if (layout->GetAttr(offset).GetTrackType() != targetTrackType) {
|
|
|
|
layout->UpdateTrackTypeAttr(offset, attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto transitions = current->GetTransitions();
|
|
|
|
if (transitions.IsUndefined()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (transitions.IsWeak()) {
|
|
|
|
auto cache = transitions.GetTaggedWeakRef();
|
|
|
|
backHClass.push(JSHClass::Cast(cache));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(transitions.IsTaggedArray());
|
|
|
|
TransitionsDictionary *dict = TransitionsDictionary::Cast(transitions.GetTaggedObject());
|
|
|
|
dict->IterateEntryValue([&backHClass] (JSHClass *cache) {
|
|
|
|
backHClass.push(JSHClass::Cast(cache));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 07:18:46 +00:00
|
|
|
TransitionResult JSHClass::ConvertOrTransitionWithRep(const JSThread *thread,
|
2023-10-31 06:47:29 +00:00
|
|
|
const JSHandle<JSObject> &receiver, const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value,
|
|
|
|
PropertyAttributes &attr)
|
|
|
|
{
|
|
|
|
auto hclass = receiver->GetJSHClass();
|
|
|
|
auto layout = LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject());
|
|
|
|
attr = layout->GetAttr(attr.GetOffset());
|
2023-12-23 03:39:48 +00:00
|
|
|
if (thread->IsPGOProfilerEnable() && !hclass->IsJSShared() && attr.UpdateTrackType(value.GetTaggedValue())) {
|
2024-07-01 13:06:12 +00:00
|
|
|
UpdateFieldType(hclass, attr);
|
2023-10-31 06:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Representation oldRep = attr.GetRepresentation();
|
|
|
|
if (oldRep == Representation::DOUBLE) {
|
|
|
|
if (value->IsInt()) {
|
|
|
|
double doubleValue = value->GetInt();
|
2024-06-13 07:18:46 +00:00
|
|
|
return {false, false, JSTaggedValue(bit_cast<JSTaggedType>(doubleValue))};
|
2023-10-31 06:47:29 +00:00
|
|
|
} else if (value->IsObject()) {
|
|
|
|
// Is Object
|
|
|
|
attr.SetRepresentation(Representation::TAGGED);
|
|
|
|
// Transition
|
|
|
|
JSHClass::TransitionForRepChange(thread, receiver, key, attr);
|
2024-06-13 07:18:46 +00:00
|
|
|
return {true, true, value.GetTaggedValue()};
|
2023-10-31 06:47:29 +00:00
|
|
|
} else {
|
|
|
|
// Is TaggedDouble
|
2024-06-13 07:18:46 +00:00
|
|
|
return {false, false, JSTaggedValue(bit_cast<JSTaggedType>(value->GetDouble()))};
|
2023-10-31 06:47:29 +00:00
|
|
|
}
|
|
|
|
} else if (oldRep == Representation::INT) {
|
|
|
|
if (value->IsInt()) {
|
|
|
|
int intValue = value->GetInt();
|
2024-06-13 07:18:46 +00:00
|
|
|
return {false, false, JSTaggedValue(static_cast<JSTaggedType>(intValue))};
|
2023-10-31 06:47:29 +00:00
|
|
|
} else {
|
|
|
|
attr.SetRepresentation(Representation::TAGGED);
|
|
|
|
JSHClass::TransitionForRepChange(thread, receiver, key, attr);
|
2024-06-13 07:18:46 +00:00
|
|
|
return {true, true, value.GetTaggedValue()};
|
2023-10-31 06:47:29 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-13 07:18:46 +00:00
|
|
|
return {true, false, value.GetTaggedValue()};
|
2023-10-31 06:47:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
JSHandle<JSTaggedValue> JSHClass::EnableProtoChangeMarker(const JSThread *thread, const JSHandle<JSHClass> &jshclass)
|
|
|
|
{
|
|
|
|
JSTaggedValue proto = jshclass->GetPrototype();
|
|
|
|
if (!proto.IsECMAObject()) {
|
|
|
|
// Return JSTaggedValue directly. No proto check is needed.
|
2023-10-31 06:47:29 +00:00
|
|
|
LOG_ECMA(INFO) << "proto is not ecmaobject: " << proto.GetRawData();
|
|
|
|
return JSHandle<JSTaggedValue>(thread, JSTaggedValue::Null());
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
JSHandle<JSObject> protoHandle(thread, proto);
|
2022-08-16 15:48:09 +00:00
|
|
|
JSHandle<JSHClass> protoClass(thread, protoHandle->GetJSHClass());
|
2023-03-08 02:22:03 +00:00
|
|
|
// in AOT's IC mechanism (VTable), when the prototype chain changes, it needs to notify each subclass
|
|
|
|
// PHC (prototype-HClass) and its IHC (instance-HClass) from the current PHC along the chain.
|
|
|
|
// therefore, when registering, it is also necessary to register IHC into its
|
|
|
|
// PHC's Listener to ensure that it can be notified.
|
2024-07-01 13:06:12 +00:00
|
|
|
RegisterOnProtoChain(thread, protoClass);
|
2023-03-08 02:22:03 +00:00
|
|
|
|
2022-08-16 15:48:09 +00:00
|
|
|
JSTaggedValue protoChangeMarker = protoClass->GetProtoChangeMarker();
|
2021-09-04 08:06:49 +00:00
|
|
|
if (protoChangeMarker.IsProtoChangeMarker()) {
|
|
|
|
JSHandle<ProtoChangeMarker> markerHandle(thread, ProtoChangeMarker::Cast(protoChangeMarker.GetTaggedObject()));
|
|
|
|
if (!markerHandle->GetHasChanged()) {
|
|
|
|
return JSHandle<JSTaggedValue>(markerHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JSHandle<ProtoChangeMarker> markerHandle = thread->GetEcmaVM()->GetFactory()->NewProtoChangeMarker();
|
|
|
|
markerHandle->SetHasChanged(false);
|
2024-05-13 14:14:07 +00:00
|
|
|
// ShareToLocal is prohibited
|
|
|
|
if (!protoClass->IsJSShared()) {
|
|
|
|
protoClass->SetProtoChangeMarker(thread, markerHandle.GetTaggedValue());
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
return JSHandle<JSTaggedValue>(markerHandle);
|
|
|
|
}
|
|
|
|
|
2023-10-31 06:47:29 +00:00
|
|
|
JSHandle<JSTaggedValue> JSHClass::EnablePHCProtoChangeMarker(const JSThread *thread,
|
|
|
|
const JSHandle<JSHClass> &protoClass)
|
|
|
|
{
|
|
|
|
RegisterOnProtoChain(thread, protoClass);
|
|
|
|
|
|
|
|
JSTaggedValue protoChangeMarker = protoClass->GetProtoChangeMarker();
|
|
|
|
if (protoChangeMarker.IsProtoChangeMarker()) {
|
|
|
|
JSHandle<ProtoChangeMarker> markerHandle(thread, ProtoChangeMarker::Cast(protoChangeMarker.GetTaggedObject()));
|
|
|
|
if (!markerHandle->GetHasChanged()) {
|
|
|
|
return JSHandle<JSTaggedValue>(markerHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JSHandle<ProtoChangeMarker> markerHandle = thread->GetEcmaVM()->GetFactory()->NewProtoChangeMarker();
|
|
|
|
markerHandle->SetHasChanged(false);
|
|
|
|
protoClass->SetProtoChangeMarker(thread, markerHandle.GetTaggedValue());
|
|
|
|
return JSHandle<JSTaggedValue>(markerHandle);
|
|
|
|
}
|
|
|
|
|
2023-05-23 03:37:28 +00:00
|
|
|
void JSHClass::NotifyHclassChanged(const JSThread *thread, JSHandle<JSHClass> oldHclass, JSHandle<JSHClass> newHclass,
|
|
|
|
JSTaggedValue addedKey)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
if (!oldHclass->IsPrototype()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The old hclass is the same as new one
|
|
|
|
if (oldHclass.GetTaggedValue() == newHclass.GetTaggedValue()) {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-17 03:26:05 +00:00
|
|
|
// For now, at pgo profiling stage, we use ProfileType::Kind to mark a hclass is CHC, PHC or IHC.
|
|
|
|
// We can have case like the following:
|
|
|
|
//
|
|
|
|
// class C3 {
|
|
|
|
// constructor(a5) {
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// class C18 extends C3 {
|
|
|
|
// constructor() {
|
|
|
|
// super(1);
|
|
|
|
// C3.valueOf = 1;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// const v37 = new C18();
|
|
|
|
//
|
|
|
|
// C3 is profiled as CHC even though its IsPrototype bit is marked as true when 'class C18 extends C3' is executed.
|
|
|
|
// Since C3 is marked as CHC and it has ProfileType::Kind::ConstructorId,
|
|
|
|
// when generating hclass at aot, its child hclass and itself will not have IsPrototype bit set as true.
|
|
|
|
//
|
|
|
|
// However, we currently support hclass substitution when executing 'C3.valueOf' for C3's oldHclass at runtime.
|
|
|
|
// Therefore, oldHclass's IsPrototype bit is set as true; But for newHclass, it is generated at aot stage,
|
|
|
|
// it will not have IsPrototype bit set as true.
|
|
|
|
//
|
|
|
|
// Good neww is our AOT hclass can not be shared, hence we can set newHclass IsPrototype as true at here.
|
2024-11-10 08:35:08 +00:00
|
|
|
if (newHclass->IsAOT() && !newHclass->IsPrototype()) {
|
2024-07-17 03:26:05 +00:00
|
|
|
newHclass->SetIsPrototype(true);
|
|
|
|
}
|
2023-05-23 03:37:28 +00:00
|
|
|
JSHClass::NoticeThroughChain(thread, oldHclass, addedKey);
|
2021-09-04 08:06:49 +00:00
|
|
|
JSHClass::RefreshUsers(thread, oldHclass, newHclass);
|
|
|
|
}
|
|
|
|
|
2023-10-31 06:47:29 +00:00
|
|
|
void JSHClass::NotifyAccessorChanged(const JSThread *thread, JSHandle<JSHClass> hclass)
|
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
JSTaggedValue markerValue = hclass->GetProtoChangeMarker();
|
|
|
|
if (markerValue.IsProtoChangeMarker()) {
|
|
|
|
ProtoChangeMarker *protoChangeMarker = ProtoChangeMarker::Cast(markerValue.GetTaggedObject());
|
|
|
|
protoChangeMarker->SetAccessorHasChanged(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSTaggedValue protoDetailsValue = hclass->GetProtoChangeDetails();
|
|
|
|
if (!protoDetailsValue.IsProtoChangeDetails()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSTaggedValue listenersValue = ProtoChangeDetails::Cast(protoDetailsValue.GetTaggedObject())->GetChangeListener();
|
|
|
|
if (!listenersValue.IsTaggedArray()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ChangeListener *listeners = ChangeListener::Cast(listenersValue.GetTaggedObject());
|
|
|
|
for (uint32_t i = 0; i < listeners->GetEnd(); i++) {
|
|
|
|
JSTaggedValue temp = listeners->Get(i);
|
|
|
|
if (temp.IsJSHClass()) {
|
|
|
|
NotifyAccessorChanged(thread, JSHandle<JSHClass>(thread, listeners->Get(i).GetTaggedObject()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
void JSHClass::RegisterOnProtoChain(const JSThread *thread, const JSHandle<JSHClass> &jshclass)
|
|
|
|
{
|
2024-05-13 14:14:07 +00:00
|
|
|
// ShareToLocal is prohibited
|
|
|
|
if (jshclass->IsJSShared()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
JSHandle<JSHClass> user = jshclass;
|
|
|
|
JSHandle<ProtoChangeDetails> userDetails = GetProtoChangeDetails(thread, user);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// Find the prototype chain as far as the hclass has not been registered.
|
2022-02-16 02:45:01 +00:00
|
|
|
if (userDetails->GetRegisterIndex() != static_cast<uint32_t>(ProtoChangeDetails::UNREGISTERED)) {
|
2021-09-04 08:06:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSTaggedValue proto = user->GetPrototype();
|
|
|
|
if (!proto.IsHeapObject()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (proto.IsJSProxy()) {
|
|
|
|
return;
|
|
|
|
}
|
2024-05-13 14:14:07 +00:00
|
|
|
// ShareToLocal is prohibited
|
|
|
|
if (proto.IsJSShared()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
ASSERT(proto.IsECMAObject());
|
|
|
|
JSHandle<JSObject> protoHandle(thread, proto);
|
|
|
|
JSHandle<ProtoChangeDetails> protoDetails =
|
|
|
|
GetProtoChangeDetails(thread, JSHandle<JSHClass>(thread, protoHandle->GetJSHClass()));
|
|
|
|
JSTaggedValue listeners = protoDetails->GetChangeListener();
|
|
|
|
JSHandle<ChangeListener> listenersHandle;
|
2022-01-25 10:05:12 +00:00
|
|
|
if (listeners.IsUndefined()) {
|
2021-09-04 08:06:49 +00:00
|
|
|
listenersHandle = JSHandle<ChangeListener>(ChangeListener::Create(thread));
|
|
|
|
} else {
|
|
|
|
listenersHandle = JSHandle<ChangeListener>(thread, listeners);
|
|
|
|
}
|
2022-01-14 13:50:26 +00:00
|
|
|
uint32_t registerIndex = 0;
|
2021-09-04 08:06:49 +00:00
|
|
|
JSHandle<ChangeListener> newListeners = ChangeListener::Add(thread, listenersHandle, user, ®isterIndex);
|
2022-01-25 10:05:12 +00:00
|
|
|
userDetails->SetRegisterIndex(registerIndex);
|
2021-09-04 08:06:49 +00:00
|
|
|
protoDetails->SetChangeListener(thread, newListeners.GetTaggedValue());
|
|
|
|
userDetails = protoDetails;
|
|
|
|
user = JSHandle<JSHClass>(thread, protoHandle->GetJSHClass());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSHClass::UnregisterOnProtoChain(const JSThread *thread, const JSHandle<JSHClass> &jshclass)
|
|
|
|
{
|
|
|
|
ASSERT(jshclass->IsPrototype());
|
|
|
|
if (!jshclass->GetProtoChangeDetails().IsProtoChangeDetails()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!jshclass->GetPrototype().IsECMAObject()) {
|
|
|
|
JSTaggedValue listeners =
|
|
|
|
ProtoChangeDetails::Cast(jshclass->GetProtoChangeDetails().GetTaggedObject())->GetChangeListener();
|
2022-01-25 10:05:12 +00:00
|
|
|
return !listeners.IsUndefined();
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
JSHandle<ProtoChangeDetails> currentDetails = GetProtoChangeDetails(thread, jshclass);
|
2022-01-25 10:05:12 +00:00
|
|
|
uint32_t index = currentDetails->GetRegisterIndex();
|
2022-02-16 02:45:01 +00:00
|
|
|
if (index == static_cast<uint32_t>(ProtoChangeDetails::UNREGISTERED)) {
|
2021-09-04 08:06:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JSTaggedValue proto = jshclass->GetPrototype();
|
|
|
|
ASSERT(proto.IsECMAObject());
|
|
|
|
JSTaggedValue protoDetailsValue = JSObject::Cast(proto.GetTaggedObject())->GetJSHClass()->GetProtoChangeDetails();
|
2024-02-26 07:08:02 +00:00
|
|
|
if (protoDetailsValue.IsUndefined() || protoDetailsValue.IsNull()) {
|
2023-11-30 02:06:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
ASSERT(protoDetailsValue.IsProtoChangeDetails());
|
|
|
|
JSTaggedValue listenersValue = ProtoChangeDetails::Cast(protoDetailsValue.GetTaggedObject())->GetChangeListener();
|
2022-01-25 10:05:12 +00:00
|
|
|
ASSERT(!listenersValue.IsUndefined());
|
2021-09-04 08:06:49 +00:00
|
|
|
JSHandle<ChangeListener> listeners(thread, listenersValue.GetTaggedObject());
|
|
|
|
ASSERT(listeners->Get(index) == jshclass.GetTaggedValue());
|
|
|
|
listeners->Delete(thread, index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSHandle<ProtoChangeDetails> JSHClass::GetProtoChangeDetails(const JSThread *thread, const JSHandle<JSHClass> &jshclass)
|
|
|
|
{
|
|
|
|
JSTaggedValue protoDetails = jshclass->GetProtoChangeDetails();
|
|
|
|
if (protoDetails.IsProtoChangeDetails()) {
|
|
|
|
return JSHandle<ProtoChangeDetails>(thread, protoDetails);
|
|
|
|
}
|
|
|
|
JSHandle<ProtoChangeDetails> protoDetailsHandle = thread->GetEcmaVM()->GetFactory()->NewProtoChangeDetails();
|
|
|
|
jshclass->SetProtoChangeDetails(thread, protoDetailsHandle.GetTaggedValue());
|
|
|
|
return protoDetailsHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSHandle<ProtoChangeDetails> JSHClass::GetProtoChangeDetails(const JSThread *thread, const JSHandle<JSObject> &obj)
|
|
|
|
{
|
|
|
|
JSHandle<JSHClass> jshclass(thread, obj->GetJSHClass());
|
|
|
|
return GetProtoChangeDetails(thread, jshclass);
|
|
|
|
}
|
|
|
|
|
2024-07-01 13:06:12 +00:00
|
|
|
void JSHClass::MarkProtoChanged([[maybe_unused]] const JSThread *thread, const JSHandle<JSHClass> &jshclass)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2023-05-23 03:37:28 +00:00
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
2024-07-01 13:06:12 +00:00
|
|
|
ASSERT(jshclass->IsPrototype());
|
2021-09-04 08:06:49 +00:00
|
|
|
JSTaggedValue markerValue = jshclass->GetProtoChangeMarker();
|
|
|
|
if (markerValue.IsProtoChangeMarker()) {
|
|
|
|
ProtoChangeMarker *protoChangeMarker = ProtoChangeMarker::Cast(markerValue.GetTaggedObject());
|
|
|
|
protoChangeMarker->SetHasChanged(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 03:37:28 +00:00
|
|
|
void JSHClass::NoticeThroughChain(const JSThread *thread, const JSHandle<JSHClass> &jshclass,
|
|
|
|
JSTaggedValue addedKey)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2023-05-23 03:37:28 +00:00
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
2024-04-09 23:24:36 +00:00
|
|
|
MarkProtoChanged(thread, jshclass);
|
2021-09-04 08:06:49 +00:00
|
|
|
JSTaggedValue protoDetailsValue = jshclass->GetProtoChangeDetails();
|
|
|
|
if (!protoDetailsValue.IsProtoChangeDetails()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSTaggedValue listenersValue = ProtoChangeDetails::Cast(protoDetailsValue.GetTaggedObject())->GetChangeListener();
|
|
|
|
if (!listenersValue.IsTaggedArray()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ChangeListener *listeners = ChangeListener::Cast(listenersValue.GetTaggedObject());
|
2022-01-14 13:50:26 +00:00
|
|
|
for (uint32_t i = 0; i < listeners->GetEnd(); i++) {
|
2021-09-04 08:06:49 +00:00
|
|
|
JSTaggedValue temp = listeners->Get(i);
|
|
|
|
if (temp.IsJSHClass()) {
|
2023-05-23 03:37:28 +00:00
|
|
|
NoticeThroughChain(thread, JSHandle<JSHClass>(thread, listeners->Get(i).GetTaggedObject()), addedKey);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSHClass::RefreshUsers(const JSThread *thread, const JSHandle<JSHClass> &oldHclass,
|
|
|
|
const JSHandle<JSHClass> &newHclass)
|
|
|
|
{
|
|
|
|
ASSERT(oldHclass->IsPrototype());
|
|
|
|
ASSERT(newHclass->IsPrototype());
|
|
|
|
bool onceRegistered = UnregisterOnProtoChain(thread, oldHclass);
|
|
|
|
|
2023-07-26 07:24:56 +00:00
|
|
|
// oldHclass is already marked. Only update newHclass.protoChangeDetails if it doesn't exist for further use.
|
|
|
|
if (!newHclass->GetProtoChangeDetails().IsProtoChangeDetails()) {
|
|
|
|
newHclass->SetProtoChangeDetails(thread, oldHclass->GetProtoChangeDetails());
|
|
|
|
}
|
2022-01-25 10:05:12 +00:00
|
|
|
oldHclass->SetProtoChangeDetails(thread, JSTaggedValue::Undefined());
|
2021-09-04 08:06:49 +00:00
|
|
|
if (onceRegistered) {
|
|
|
|
if (newHclass->GetProtoChangeDetails().IsProtoChangeDetails()) {
|
|
|
|
ProtoChangeDetails::Cast(newHclass->GetProtoChangeDetails().GetTaggedObject())
|
2022-01-25 10:05:12 +00:00
|
|
|
->SetRegisterIndex(ProtoChangeDetails::UNREGISTERED);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
RegisterOnProtoChain(thread, newHclass);
|
|
|
|
}
|
|
|
|
}
|
2023-03-08 02:22:03 +00:00
|
|
|
|
2023-06-20 09:27:19 +00:00
|
|
|
PropertyLookupResult JSHClass::LookupPropertyInAotHClass(const JSThread *thread, JSHClass *hclass, JSTaggedValue key)
|
2023-03-17 05:25:20 +00:00
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
2024-11-10 08:35:08 +00:00
|
|
|
ASSERT(hclass->IsAOT());
|
2023-03-17 05:25:20 +00:00
|
|
|
|
|
|
|
PropertyLookupResult result;
|
2023-10-31 06:47:29 +00:00
|
|
|
if (hclass->IsDictionaryMode()) {
|
|
|
|
// not fuond
|
|
|
|
result.SetIsFound(false);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-17 05:25:20 +00:00
|
|
|
int entry = JSHClass::FindPropertyEntry(thread, hclass, key);
|
|
|
|
// found in local
|
|
|
|
if (entry != -1) {
|
|
|
|
result.SetIsFound(true);
|
|
|
|
result.SetIsLocal(true);
|
2023-05-16 11:24:47 +00:00
|
|
|
PropertyAttributes attr = LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject())->GetAttr(entry);
|
2023-10-31 06:47:29 +00:00
|
|
|
if (attr.IsInlinedProps()) {
|
|
|
|
result.SetIsInlinedProps(true);
|
|
|
|
result.SetOffset(hclass->GetInlinedPropertiesOffset(entry));
|
|
|
|
} else {
|
|
|
|
result.SetIsInlinedProps(false);
|
|
|
|
result.SetOffset(attr.GetOffset() - hclass->GetInlinedProperties());
|
|
|
|
}
|
2023-05-16 11:24:47 +00:00
|
|
|
if (attr.IsNotHole()) {
|
|
|
|
result.SetIsNotHole(true);
|
|
|
|
}
|
2023-06-09 03:42:55 +00:00
|
|
|
if (attr.IsAccessor()) {
|
|
|
|
result.SetIsAccessor(true);
|
|
|
|
}
|
2023-07-12 10:42:42 +00:00
|
|
|
result.SetRepresentation(attr.GetRepresentation());
|
2023-06-20 09:27:19 +00:00
|
|
|
result.SetIsWritable(attr.IsWritable());
|
2023-03-17 05:25:20 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-17 09:52:49 +00:00
|
|
|
// not found
|
2023-03-17 05:25:20 +00:00
|
|
|
result.SetIsFound(false);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-10-31 06:47:29 +00:00
|
|
|
PropertyLookupResult JSHClass::LookupPropertyInPGOHClass(const JSThread *thread, JSHClass *hclass, JSTaggedValue key)
|
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
|
|
|
|
PropertyLookupResult result;
|
|
|
|
if (hclass->IsDictionaryMode()) {
|
|
|
|
result.SetIsFound(false);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int entry = JSHClass::FindPropertyEntry(thread, hclass, key);
|
|
|
|
// found in local
|
|
|
|
if (entry != -1) {
|
|
|
|
result.SetIsFound(true);
|
|
|
|
result.SetIsLocal(true);
|
|
|
|
PropertyAttributes attr = LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject())->GetAttr(entry);
|
|
|
|
if (attr.IsInlinedProps()) {
|
|
|
|
result.SetIsInlinedProps(true);
|
|
|
|
result.SetOffset(hclass->GetInlinedPropertiesOffset(entry));
|
|
|
|
} else {
|
|
|
|
result.SetIsInlinedProps(false);
|
|
|
|
result.SetOffset(attr.GetOffset() - hclass->GetInlinedProperties());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr.IsNotHole()) {
|
|
|
|
result.SetIsNotHole(true);
|
|
|
|
}
|
|
|
|
if (attr.IsAccessor()) {
|
|
|
|
result.SetIsAccessor(true);
|
|
|
|
}
|
|
|
|
result.SetRepresentation(attr.GetRepresentation());
|
|
|
|
result.SetIsWritable(attr.IsWritable());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not fuond
|
|
|
|
result.SetIsFound(false);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:14:18 +00:00
|
|
|
PropertyLookupResult JSHClass::LookupPropertyInBuiltinPrototypeHClass(const JSThread *thread, JSHClass *hclass,
|
|
|
|
JSTaggedValue key)
|
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
ASSERT(hclass->IsPrototype());
|
|
|
|
|
|
|
|
PropertyLookupResult result;
|
2023-10-31 06:47:29 +00:00
|
|
|
if (hclass->IsDictionaryMode()) {
|
|
|
|
// not fuond
|
|
|
|
result.SetIsFound(false);
|
|
|
|
return result;
|
|
|
|
}
|
2023-08-31 12:14:18 +00:00
|
|
|
int entry = JSHClass::FindPropertyEntry(thread, hclass, key);
|
|
|
|
// When the property is not found, the value of 'entry' is -1.
|
|
|
|
// Currently, not all methods on the prototype of 'builtin' have been changed to inlined.
|
|
|
|
// Therefore, when a non-inlined method is encountered, it is also considered not found.
|
|
|
|
if (entry == -1 || static_cast<uint32_t>(entry) >= hclass->GetInlinedProperties()) {
|
|
|
|
result.SetIsFound(false);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.SetIsFound(true);
|
|
|
|
result.SetIsLocal(true);
|
|
|
|
PropertyAttributes attr = LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject())->GetAttr(entry);
|
2023-10-31 06:47:29 +00:00
|
|
|
if (attr.IsInlinedProps()) {
|
|
|
|
result.SetIsInlinedProps(true);
|
|
|
|
result.SetOffset(hclass->GetInlinedPropertiesOffset(entry));
|
|
|
|
} else {
|
|
|
|
result.SetIsInlinedProps(false);
|
|
|
|
result.SetOffset(attr.GetOffset() - hclass->GetInlinedProperties());
|
|
|
|
}
|
2023-08-31 12:14:18 +00:00
|
|
|
result.SetIsNotHole(true);
|
|
|
|
if (attr.IsAccessor()) {
|
|
|
|
result.SetIsAccessor(true);
|
|
|
|
}
|
|
|
|
result.SetRepresentation(attr.GetRepresentation());
|
|
|
|
result.SetIsWritable(attr.IsWritable());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-04-07 16:37:58 +00:00
|
|
|
JSHandle<JSTaggedValue> JSHClass::ParseKeyFromPGOCString(ObjectFactory* factory,
|
|
|
|
const CString& cstring,
|
|
|
|
const PGOHandler& handler)
|
|
|
|
{
|
|
|
|
if (handler.GetIsSymbol()) {
|
|
|
|
JSHandle<JSSymbol> symbol;
|
|
|
|
auto str = cstring.substr(0, 6); // `method` length is 6
|
|
|
|
if (str == "method") { // cstring is `method_0ULL` after _ is private id of symbol
|
|
|
|
symbol = factory->NewPublicSymbolWithChar("method");
|
2024-05-07 10:19:04 +00:00
|
|
|
ASSERT(cstring.size() > 0);
|
2024-04-07 16:37:58 +00:00
|
|
|
str = cstring.substr(7, cstring.size() - 1); // `method_` length is 7
|
|
|
|
symbol->SetPrivateId(CStringToULL(str));
|
|
|
|
} else { // cstring is private id of symbol
|
|
|
|
symbol = factory->NewJSSymbol();
|
|
|
|
symbol->SetPrivateId(CStringToULL(cstring));
|
|
|
|
}
|
|
|
|
return JSHandle<JSTaggedValue>(symbol);
|
|
|
|
} else {
|
|
|
|
return JSHandle<JSTaggedValue>(factory->NewFromStdString(std::string(cstring)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSHandle<JSHClass> JSHClass::CreateRootHClassFromPGO(const JSThread* thread,
|
|
|
|
const HClassLayoutDesc* desc,
|
2024-06-22 08:38:43 +00:00
|
|
|
uint32_t maxNum,
|
|
|
|
bool isCache)
|
2023-10-31 06:47:29 +00:00
|
|
|
{
|
2024-06-22 08:38:43 +00:00
|
|
|
if (isCache) {
|
|
|
|
if (ObjectFactory::CanObjectLiteralHClassCache(maxNum)) {
|
|
|
|
return CreateRootHClassWithCached(thread, desc, maxNum);
|
|
|
|
}
|
|
|
|
}
|
2023-10-31 06:47:29 +00:00
|
|
|
auto rootDesc = reinterpret_cast<const pgo::RootHClassLayoutDesc *>(desc);
|
|
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
|
|
uint32_t numOfProps = rootDesc->NumOfProps();
|
|
|
|
uint32_t index = 0;
|
|
|
|
JSType type = rootDesc->GetObjectType();
|
|
|
|
size_t size = rootDesc->GetObjectSize();
|
|
|
|
JSHandle<JSHClass> hclass = factory->NewEcmaHClass(size, type, maxNum);
|
|
|
|
// Dictionary?
|
2023-12-28 09:27:25 +00:00
|
|
|
JSHandle<LayoutInfo> layout = factory->CreateLayoutInfo(maxNum, MemSpaceType::SEMI_SPACE, GrowMode::KEEP);
|
2024-04-07 16:37:58 +00:00
|
|
|
rootDesc->IterateProps([thread, factory, &index, hclass, layout](const pgo::PropertyDesc& propDesc) {
|
|
|
|
auto& cstring = propDesc.first;
|
|
|
|
auto& handler = propDesc.second;
|
|
|
|
JSHandle<JSTaggedValue> key = ParseKeyFromPGOCString(factory, cstring, handler);
|
2023-10-31 06:47:29 +00:00
|
|
|
PropertyAttributes attributes = PropertyAttributes::Default();
|
2024-04-07 16:37:58 +00:00
|
|
|
if (handler.SetAttribute(thread, attributes)) {
|
2023-10-31 06:47:29 +00:00
|
|
|
hclass->SetIsAllTaggedProp(false);
|
|
|
|
}
|
|
|
|
attributes.SetIsInlinedProps(true);
|
|
|
|
attributes.SetOffset(index);
|
|
|
|
layout->AddKey(thread, index, key.GetTaggedValue(), attributes);
|
|
|
|
index++;
|
|
|
|
});
|
|
|
|
hclass->SetLayout(thread, layout);
|
|
|
|
hclass->SetNumberOfProps(numOfProps);
|
2024-11-10 08:35:08 +00:00
|
|
|
hclass->SetAOT(true);
|
2023-10-31 06:47:29 +00:00
|
|
|
return hclass;
|
|
|
|
}
|
|
|
|
|
2024-06-22 08:38:43 +00:00
|
|
|
JSHandle<JSHClass> JSHClass::CreateRootHClassWithCached(const JSThread* thread,
|
|
|
|
const HClassLayoutDesc* desc,
|
|
|
|
uint32_t maxNum)
|
|
|
|
{
|
|
|
|
auto rootDesc = reinterpret_cast<const pgo::RootHClassLayoutDesc *>(desc);
|
|
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
|
|
uint32_t index = 0;
|
|
|
|
ASSERT(rootDesc->GetObjectSize() == JSObject::SIZE);
|
|
|
|
ASSERT(rootDesc->GetObjectType() == JSType::JS_OBJECT);
|
|
|
|
JSHandle<JSHClass> hclass = factory->GetObjectLiteralRootHClass(maxNum);
|
|
|
|
JSHandle<LayoutInfo> layout = factory->CreateLayoutInfo(maxNum, MemSpaceType::SEMI_SPACE, GrowMode::KEEP);
|
|
|
|
hclass->SetPrototype(thread, JSTaggedValue::Null());
|
|
|
|
hclass->SetLayout(thread, layout);
|
2024-11-10 08:35:08 +00:00
|
|
|
hclass->SetAOT(true);
|
2024-06-22 08:38:43 +00:00
|
|
|
rootDesc->IterateProps([thread, factory, &index, &hclass](const pgo::PropertyDesc& propDesc) {
|
|
|
|
auto& cstring = propDesc.first;
|
|
|
|
auto& handler = propDesc.second;
|
|
|
|
JSHandle<JSTaggedValue> key = ParseKeyFromPGOCString(factory, cstring, handler);
|
|
|
|
PropertyAttributes attributes = PropertyAttributes::Default();
|
|
|
|
if (handler.SetAttribute(thread, attributes)) {
|
|
|
|
hclass->SetIsAllTaggedProp(false);
|
|
|
|
}
|
|
|
|
attributes.SetIsInlinedProps(true);
|
|
|
|
attributes.SetOffset(index++);
|
2024-07-09 12:34:51 +00:00
|
|
|
auto rep = attributes.GetRepresentation();
|
2024-06-22 08:38:43 +00:00
|
|
|
|
2024-07-09 12:34:51 +00:00
|
|
|
JSHandle<JSHClass> child = SetPropertyOfObjHClass(thread, hclass, key, attributes, rep);
|
2024-06-22 08:38:43 +00:00
|
|
|
child->SetParent(thread, hclass);
|
|
|
|
child->SetPrototype(thread, JSTaggedValue::Null());
|
2024-11-10 08:35:08 +00:00
|
|
|
child->SetAOT(true);
|
2024-06-22 08:38:43 +00:00
|
|
|
hclass = child;
|
|
|
|
});
|
|
|
|
return hclass;
|
|
|
|
}
|
|
|
|
|
2024-04-07 16:37:58 +00:00
|
|
|
JSHandle<JSHClass> JSHClass::CreateChildHClassFromPGO(const JSThread* thread,
|
|
|
|
const JSHandle<JSHClass>& parent,
|
|
|
|
const HClassLayoutDesc* desc)
|
2023-10-31 06:47:29 +00:00
|
|
|
{
|
|
|
|
pgo::PropertyDesc propDesc = reinterpret_cast<const pgo::ChildHClassLayoutDesc *>(desc)->GetPropertyDesc();
|
2024-04-07 16:37:58 +00:00
|
|
|
auto& cstring = propDesc.first;
|
|
|
|
auto& handler = propDesc.second;
|
2023-10-31 06:47:29 +00:00
|
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
|
|
uint32_t numOfProps = parent->NumberOfProps();
|
|
|
|
|
|
|
|
JSHandle<JSHClass> newJsHClass = JSHClass::Clone(thread, parent);
|
2024-11-10 08:35:08 +00:00
|
|
|
newJsHClass->SetAOT(true);
|
2023-10-31 06:47:29 +00:00
|
|
|
ASSERT(newJsHClass->GetInlinedProperties() >= (numOfProps + 1));
|
|
|
|
uint32_t offset = numOfProps;
|
|
|
|
{
|
|
|
|
JSMutableHandle<LayoutInfo> layoutInfoHandle(thread, newJsHClass->GetLayout());
|
|
|
|
if (layoutInfoHandle->NumberOfElements() != static_cast<int>(offset)) {
|
|
|
|
layoutInfoHandle.Update(factory->CopyAndReSort(layoutInfoHandle, offset, offset + 1));
|
|
|
|
newJsHClass->SetLayout(thread, layoutInfoHandle);
|
|
|
|
} else if (layoutInfoHandle->GetPropertiesCapacity() <= static_cast<int>(offset)) { // need to Grow
|
|
|
|
layoutInfoHandle.Update(
|
|
|
|
factory->ExtendLayoutInfo(layoutInfoHandle, offset));
|
|
|
|
newJsHClass->SetLayout(thread, layoutInfoHandle);
|
|
|
|
}
|
2024-04-07 16:37:58 +00:00
|
|
|
JSHandle<JSTaggedValue> key = ParseKeyFromPGOCString(factory, cstring, handler);
|
2023-10-31 06:47:29 +00:00
|
|
|
PropertyAttributes attributes = PropertyAttributes::Default();
|
2024-04-07 16:37:58 +00:00
|
|
|
if (handler.SetAttribute(thread, attributes)) {
|
2023-10-31 06:47:29 +00:00
|
|
|
newJsHClass->SetIsAllTaggedProp(false);
|
|
|
|
}
|
|
|
|
attributes.SetOffset(offset);
|
|
|
|
attributes.SetIsInlinedProps(true);
|
|
|
|
layoutInfoHandle->AddKey(thread, offset, key.GetTaggedValue(), attributes);
|
|
|
|
newJsHClass->IncNumberOfProps();
|
2024-04-07 16:37:58 +00:00
|
|
|
AddTransitions(thread, parent, newJsHClass, key, attributes);
|
2023-10-31 06:47:29 +00:00
|
|
|
JSHClass::NotifyHclassChanged(thread, parent, newJsHClass, key.GetTaggedValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
return newJsHClass;
|
|
|
|
}
|
|
|
|
|
2024-04-07 16:37:58 +00:00
|
|
|
bool JSHClass::DumpRootHClassByPGO(const JSHClass* hclass, HClassLayoutDesc* desc)
|
2023-05-15 08:48:14 +00:00
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
2023-06-01 08:27:41 +00:00
|
|
|
if (hclass->IsDictionaryMode()) {
|
2023-05-15 08:48:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-01 08:27:41 +00:00
|
|
|
LayoutInfo *layout = LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject());
|
2023-06-21 06:35:02 +00:00
|
|
|
int element = static_cast<int>(hclass->NumberOfProps());
|
2023-05-15 08:48:14 +00:00
|
|
|
for (int i = 0; i < element; i++) {
|
2024-04-07 16:37:58 +00:00
|
|
|
layout->DumpFieldIndexByPGO(i, desc);
|
2023-10-31 06:47:29 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-07 16:37:58 +00:00
|
|
|
bool JSHClass::DumpChildHClassByPGO(const JSHClass* hclass, HClassLayoutDesc* desc)
|
2023-10-31 06:47:29 +00:00
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
if (hclass->IsDictionaryMode()) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-12 11:02:26 +00:00
|
|
|
if (hclass->PropsIsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint32_t last = hclass->LastPropIndex();
|
2023-10-31 06:47:29 +00:00
|
|
|
LayoutInfo *layoutInfo = LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject());
|
2024-04-07 16:37:58 +00:00
|
|
|
layoutInfo->DumpFieldIndexByPGO(last, desc);
|
2023-10-31 06:47:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-07 16:37:58 +00:00
|
|
|
bool JSHClass::UpdateChildLayoutDescByPGO(const JSHClass* hclass, HClassLayoutDesc* childDesc)
|
2023-10-31 06:47:29 +00:00
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
if (hclass->IsDictionaryMode()) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-12 11:02:26 +00:00
|
|
|
if (hclass->PropsIsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint32_t last = hclass->LastPropIndex();
|
2023-10-31 06:47:29 +00:00
|
|
|
LayoutInfo *layoutInfo = LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject());
|
2024-04-07 16:37:58 +00:00
|
|
|
return layoutInfo->UpdateFieldIndexByPGO(last, childDesc);
|
2023-10-31 06:47:29 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 13:06:12 +00:00
|
|
|
bool JSHClass::UpdateRootLayoutDescByPGO(const JSHClass* hclass, HClassLayoutDesc* desc)
|
2023-10-31 06:47:29 +00:00
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
if (hclass->IsDictionaryMode()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rootDesc = reinterpret_cast<const pgo::RootHClassLayoutDesc *>(desc);
|
2023-11-25 08:45:19 +00:00
|
|
|
int rootPropLen = static_cast<int>(rootDesc->NumOfProps());
|
2023-10-31 06:47:29 +00:00
|
|
|
LayoutInfo *layout = LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject());
|
|
|
|
for (int i = 0; i < rootPropLen; i++) {
|
2024-04-07 16:37:58 +00:00
|
|
|
layout->UpdateFieldIndexByPGO(i, desc);
|
2023-10-31 06:47:29 +00:00
|
|
|
}
|
2023-06-01 08:27:41 +00:00
|
|
|
return true;
|
2023-05-15 08:48:14 +00:00
|
|
|
}
|
2023-08-21 12:11:01 +00:00
|
|
|
|
2023-10-31 06:47:29 +00:00
|
|
|
CString JSHClass::DumpToString(JSTaggedType hclassVal)
|
2023-08-21 12:11:01 +00:00
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
2023-10-31 06:47:29 +00:00
|
|
|
auto hclass = JSHClass::Cast(JSTaggedValue(hclassVal).GetTaggedObject());
|
2023-08-21 12:11:01 +00:00
|
|
|
if (hclass->IsDictionaryMode()) {
|
2023-10-31 06:47:29 +00:00
|
|
|
return "";
|
2023-08-21 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CString result;
|
|
|
|
LayoutInfo *layout = LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject());
|
|
|
|
int element = static_cast<int>(hclass->NumberOfProps());
|
|
|
|
for (int i = 0; i < element; i++) {
|
|
|
|
auto key = layout->GetKey(i);
|
|
|
|
if (key.IsString()) {
|
2024-07-01 13:06:12 +00:00
|
|
|
uint64_t value = EcmaStringAccessor(key).GetHashcode();
|
|
|
|
value <<= sizeof(uint32_t) * BITS_PER_BYTE;
|
2023-08-21 12:11:01 +00:00
|
|
|
auto attr = layout->GetAttr(i);
|
2024-07-01 13:06:12 +00:00
|
|
|
auto defaultAttr = PropertyAttributes(attr.GetPropertyMetaData());
|
|
|
|
defaultAttr.SetTrackType(attr.GetTrackType());
|
|
|
|
value += defaultAttr.GetValue();
|
|
|
|
result += ToCString(value);
|
2023-11-22 01:15:28 +00:00
|
|
|
} else if (key.IsSymbol()) {
|
2024-04-07 16:37:58 +00:00
|
|
|
result += JSSymbol::Cast(key)->GetPrivateId();
|
|
|
|
auto attr = layout->GetAttr(i);
|
|
|
|
result += static_cast<int32_t>(attr.GetTrackType());
|
|
|
|
result += attr.GetPropertyMetaData();
|
2023-11-22 01:15:28 +00:00
|
|
|
} else {
|
|
|
|
LOG_ECMA(FATAL) << "JSHClass::DumpToString UNREACHABLE";
|
2023-08-21 12:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-31 06:47:29 +00:00
|
|
|
return result;
|
2023-08-21 12:11:01 +00:00
|
|
|
}
|
2023-12-27 14:12:51 +00:00
|
|
|
|
|
|
|
PropertyLookupResult JSHClass::LookupPropertyInBuiltinHClass(const JSThread *thread, JSHClass *hclass,
|
|
|
|
JSTaggedValue key)
|
|
|
|
{
|
|
|
|
DISALLOW_GARBAGE_COLLECTION;
|
|
|
|
|
|
|
|
PropertyLookupResult result;
|
|
|
|
if (hclass->IsDictionaryMode()) {
|
|
|
|
result.SetIsFound(false);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int entry = JSHClass::FindPropertyEntry(thread, hclass, key);
|
|
|
|
// found in local
|
|
|
|
if (entry != -1) {
|
|
|
|
result.SetIsFound(true);
|
|
|
|
result.SetIsLocal(true);
|
|
|
|
PropertyAttributes attr = LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject())->GetAttr(entry);
|
|
|
|
if (attr.IsInlinedProps()) {
|
|
|
|
result.SetIsInlinedProps(true);
|
|
|
|
result.SetOffset(hclass->GetInlinedPropertiesOffset(entry));
|
|
|
|
} else {
|
|
|
|
result.SetIsInlinedProps(false);
|
|
|
|
result.SetOffset(attr.GetOffset() - hclass->GetInlinedProperties());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr.IsNotHole()) {
|
|
|
|
result.SetIsNotHole(true);
|
|
|
|
}
|
|
|
|
if (attr.IsAccessor()) {
|
|
|
|
result.SetIsAccessor(true);
|
|
|
|
}
|
|
|
|
result.SetRepresentation(attr.GetRepresentation());
|
|
|
|
result.SetIsWritable(attr.IsWritable());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not fuond
|
|
|
|
result.SetIsFound(false);
|
|
|
|
return result;
|
|
|
|
}
|
2024-03-22 12:02:22 +00:00
|
|
|
|
|
|
|
JSHandle<JSHClass> JSHClass::CreateSHClass(JSThread *thread,
|
|
|
|
const std::vector<PropertyDescriptor> &descs,
|
2024-04-20 02:26:26 +00:00
|
|
|
const JSHClass *parentHClass,
|
2024-03-22 12:02:22 +00:00
|
|
|
bool isFunction)
|
|
|
|
{
|
|
|
|
EcmaVM *vm = thread->GetEcmaVM();
|
|
|
|
ObjectFactory *factory = vm->GetFactory();
|
|
|
|
|
|
|
|
uint32_t length = descs.size();
|
|
|
|
uint32_t maxInline = isFunction ? JSSharedFunction::MAX_INLINE : JSSharedObject::MAX_INLINE;
|
|
|
|
|
2024-04-20 02:26:26 +00:00
|
|
|
if (parentHClass) {
|
|
|
|
if (parentHClass->IsDictionaryMode()) {
|
|
|
|
auto dict = reinterpret_cast<NameDictionary *>(parentHClass->GetLayout().GetTaggedObject());
|
2024-05-31 07:07:53 +00:00
|
|
|
length += static_cast<uint32_t>(dict->EntriesCount());
|
2024-04-20 02:26:26 +00:00
|
|
|
} else {
|
|
|
|
length += parentHClass->NumberOfProps();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 12:02:22 +00:00
|
|
|
JSHandle<JSHClass> hclass =
|
|
|
|
isFunction ? factory->NewSEcmaHClass(JSSharedFunction::SIZE, JSType::JS_SHARED_FUNCTION, length)
|
|
|
|
: factory->NewSEcmaHClass(JSSharedObject::SIZE, JSType::JS_SHARED_OBJECT, length);
|
|
|
|
if (LIKELY(length <= maxInline)) {
|
2024-04-20 02:26:26 +00:00
|
|
|
CreateSInlinedLayout(thread, descs, hclass, parentHClass);
|
2024-03-22 12:02:22 +00:00
|
|
|
} else {
|
2024-04-20 02:26:26 +00:00
|
|
|
CreateSDictLayout(thread, descs, hclass, parentHClass);
|
2024-03-22 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hclass;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSHandle<JSHClass> JSHClass::CreateSConstructorHClass(JSThread *thread, const std::vector<PropertyDescriptor> &descs)
|
|
|
|
{
|
2024-04-20 02:26:26 +00:00
|
|
|
auto hclass = CreateSHClass(thread, descs, nullptr, true);
|
2024-03-22 12:02:22 +00:00
|
|
|
hclass->SetClassConstructor(true);
|
|
|
|
hclass->SetConstructor(true);
|
|
|
|
return hclass;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSHandle<JSHClass> JSHClass::CreateSPrototypeHClass(JSThread *thread, const std::vector<PropertyDescriptor> &descs)
|
|
|
|
{
|
|
|
|
auto hclass = CreateSHClass(thread, descs);
|
|
|
|
hclass->SetClassPrototype(true);
|
|
|
|
hclass->SetIsPrototype(true);
|
|
|
|
return hclass;
|
|
|
|
}
|
|
|
|
|
2024-04-20 02:26:26 +00:00
|
|
|
void JSHClass::CreateSInlinedLayout(JSThread *thread,
|
|
|
|
const std::vector<PropertyDescriptor> &descs,
|
|
|
|
const JSHandle<JSHClass> &hclass,
|
|
|
|
const JSHClass *parentHClass)
|
2024-03-22 12:02:22 +00:00
|
|
|
{
|
|
|
|
EcmaVM *vm = thread->GetEcmaVM();
|
|
|
|
ObjectFactory *factory = vm->GetFactory();
|
|
|
|
|
2024-04-20 02:26:26 +00:00
|
|
|
uint32_t parentLength{0};
|
|
|
|
if (parentHClass) {
|
|
|
|
parentLength = parentHClass->NumberOfProps();
|
|
|
|
}
|
2024-03-22 12:02:22 +00:00
|
|
|
auto length = descs.size();
|
2024-04-20 02:26:26 +00:00
|
|
|
auto layout = factory->CreateSLayoutInfo(length + parentLength);
|
2024-03-22 12:02:22 +00:00
|
|
|
|
2024-04-20 02:26:26 +00:00
|
|
|
JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
|
2024-03-22 12:02:22 +00:00
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
|
|
|
key.Update(descs[i].GetKey());
|
|
|
|
PropertyAttributes attr =
|
|
|
|
PropertyAttributes::Default(descs[i].IsWritable(), descs[i].IsEnumerable(), descs[i].IsConfigurable());
|
|
|
|
if (UNLIKELY(descs[i].GetValue()->IsAccessor())) {
|
|
|
|
attr.SetIsAccessor(true);
|
|
|
|
}
|
|
|
|
attr.SetIsInlinedProps(true);
|
|
|
|
attr.SetRepresentation(Representation::TAGGED);
|
|
|
|
attr.SetSharedFieldType(descs[i].GetSharedFieldType());
|
|
|
|
attr.SetOffset(i);
|
|
|
|
layout->AddKey(thread, i, key.GetTaggedValue(), attr);
|
|
|
|
}
|
|
|
|
|
2024-04-20 02:26:26 +00:00
|
|
|
auto index = length;
|
|
|
|
if (parentHClass) {
|
|
|
|
JSHandle<LayoutInfo> old(thread, parentHClass->GetLayout());
|
|
|
|
for (uint32_t i = 0; i < parentLength; i++) {
|
2024-05-28 13:55:12 +00:00
|
|
|
key.Update(old->GetKey(i));
|
2024-04-20 02:26:26 +00:00
|
|
|
auto entry = layout->FindElementWithCache(thread, *hclass, key.GetTaggedValue(), index);
|
|
|
|
if (entry != -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto attr = PropertyAttributes(old->GetAttr(i));
|
|
|
|
attr.SetOffset(index);
|
|
|
|
layout->AddKey(thread, index, old->GetKey(i), attr);
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hclass->SetLayout(thread, layout);
|
|
|
|
hclass->SetNumberOfProps(index);
|
|
|
|
auto inlinedPropsLength = hclass->GetInlinedProperties();
|
|
|
|
if (inlinedPropsLength > index) {
|
|
|
|
uint32_t duplicatedSize = (inlinedPropsLength - index) * JSTaggedValue::TaggedTypeSize();
|
|
|
|
hclass->SetObjectSize(hclass->GetObjectSize() - duplicatedSize);
|
|
|
|
}
|
2024-03-22 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
2024-04-20 02:26:26 +00:00
|
|
|
void JSHClass::CreateSDictLayout(JSThread *thread,
|
|
|
|
const std::vector<PropertyDescriptor> &descs,
|
|
|
|
const JSHandle<JSHClass> &hclass,
|
|
|
|
const JSHClass *parentHClass)
|
2024-03-22 12:02:22 +00:00
|
|
|
{
|
2024-04-20 02:26:26 +00:00
|
|
|
uint32_t parentLength{0};
|
|
|
|
if (parentHClass) {
|
|
|
|
if (parentHClass->IsDictionaryMode()) {
|
2024-05-31 07:07:53 +00:00
|
|
|
parentLength = static_cast<uint32_t>(
|
|
|
|
reinterpret_cast<NameDictionary *>(parentHClass->GetLayout().GetTaggedObject())->EntriesCount());
|
2024-04-20 02:26:26 +00:00
|
|
|
} else {
|
|
|
|
parentLength = parentHClass->NumberOfProps();
|
|
|
|
}
|
|
|
|
}
|
2024-03-22 12:02:22 +00:00
|
|
|
auto length = descs.size();
|
|
|
|
JSMutableHandle<NameDictionary> dict(
|
2024-04-20 02:26:26 +00:00
|
|
|
thread,
|
|
|
|
NameDictionary::CreateInSharedHeap(thread, NameDictionary::ComputeHashTableSize(length + parentLength)));
|
2024-03-22 12:02:22 +00:00
|
|
|
JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
|
|
|
|
auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
|
|
|
|
JSHandle<JSTaggedValue> value = globalConst->GetHandledUndefined();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
|
|
|
key.Update(descs[i].GetKey());
|
|
|
|
PropertyAttributes attr =
|
|
|
|
PropertyAttributes::Default(descs[i].IsWritable(), descs[i].IsEnumerable(), descs[i].IsConfigurable());
|
|
|
|
attr.SetSharedFieldType(descs[i].GetSharedFieldType());
|
|
|
|
attr.SetBoxType(PropertyBoxType::UNDEFINED);
|
|
|
|
JSHandle<NameDictionary> newDict = NameDictionary::Put(thread, dict, key, value, attr);
|
|
|
|
dict.Update(newDict);
|
|
|
|
}
|
|
|
|
|
2024-04-20 02:26:26 +00:00
|
|
|
if (parentHClass) {
|
|
|
|
if (parentHClass->IsDictionaryMode()) {
|
|
|
|
JSHandle<NameDictionary> old(thread, parentHClass->GetLayout());
|
|
|
|
std::vector<int> indexOrder = old->GetEnumerationOrder();
|
|
|
|
for (uint32_t i = 0; i < parentLength; i++) {
|
|
|
|
key.Update(old->GetKey(indexOrder[i]));
|
|
|
|
JSHandle<NameDictionary> newDict = NameDictionary::Put(
|
|
|
|
thread, dict, key, value, PropertyAttributes(old->GetAttributes(indexOrder[i])));
|
|
|
|
dict.Update(newDict);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JSHandle<LayoutInfo> old(thread, parentHClass->GetLayout());
|
|
|
|
for (uint32_t i = 0; i < parentLength; i++) {
|
|
|
|
key.Update(old->GetKey(i));
|
|
|
|
JSHandle<NameDictionary> newDict =
|
|
|
|
NameDictionary::Put(thread, dict, key, value, PropertyAttributes(old->GetAttr(i)));
|
|
|
|
dict.Update(newDict);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hclass->SetLayout(thread, dict);
|
|
|
|
hclass->SetNumberOfProps(0);
|
|
|
|
hclass->SetIsDictionaryMode(true);
|
2024-03-22 12:02:22 +00:00
|
|
|
}
|
2024-07-31 02:41:31 +00:00
|
|
|
bool JSHClass::IsNeedNotifyHclassChangedForAotTransition(const JSThread *thread, const JSHandle<JSHClass> &hclass,
|
|
|
|
JSTaggedValue key)
|
|
|
|
{
|
|
|
|
JSMutableHandle<JSObject> protoHandle(thread, hclass->GetPrototype());
|
|
|
|
while (protoHandle.GetTaggedValue().IsHeapObject()) {
|
|
|
|
JSHClass *protoHclass = protoHandle->GetJSHClass();
|
|
|
|
if (JSHClass::FindPropertyEntry(thread, protoHclass, key) != -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
protoHandle.Update(protoHclass->GetPrototype());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
} // namespace panda::ecmascript
|