1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "ecmascript/ts_types/ts_obj_layout_info.h"
16
17 namespace panda::ecmascript {
AddProperty(const JSThread * thread,const JSTaggedValue key,const JSTaggedValue typeIdVal,const uint32_t attr)18 void TSObjLayoutInfo::AddProperty(const JSThread *thread, const JSTaggedValue key,
19 const JSTaggedValue typeIdVal, const uint32_t attr)
20 {
21 AddProperty(thread, key, typeIdVal, JSTaggedValue(attr));
22 }
23
AddProperty(const JSThread * thread,const JSTaggedValue key,const JSTaggedValue typeIdVal,const JSTaggedValue attr)24 void TSObjLayoutInfo::AddProperty(const JSThread *thread, const JSTaggedValue key,
25 const JSTaggedValue typeIdVal, const JSTaggedValue attr)
26 {
27 DISALLOW_GARBAGE_COLLECTION;
28 uint32_t number = GetNumOfProperties();
29 ASSERT(number + 1 <= GetPropertiesCapacity());
30 ASSERT(attr.IsInt());
31 SetNumOfProperties(thread, number + 1);
32 SetKey(thread, number, key);
33 SetTypeId(thread, number, typeIdVal);
34 SetAttribute(thread, number, attr);
35 }
36
Find(JSTaggedValue key) const37 bool TSObjLayoutInfo::Find(JSTaggedValue key) const
38 {
39 return GetElementIndexByKey(key) != INVALID_INDEX;
40 }
41
GetElementIndexByKey(JSTaggedValue key) const42 int TSObjLayoutInfo::GetElementIndexByKey(JSTaggedValue key) const
43 {
44 [[maybe_unused]] EcmaString *str = EcmaString::Cast(key.GetTaggedObject());
45 ASSERT_PRINT(EcmaStringAccessor(str).IsInternString(), "TS class field key is not an intern string");
46
47 uint32_t length = GetNumOfProperties();
48 for (uint32_t i = 0; i < length; ++i) {
49 JSTaggedValue keyVal = GetKey(i);
50 ASSERT_PRINT(keyVal.IsString(), "TS class field key is not a string");
51 if (keyVal == key) {
52 return i;
53 }
54 }
55 return INVALID_INDEX;
56 }
57
GetAccessorIndexByKey(JSTaggedValue key,std::vector<int> & vec)58 void TSObjLayoutInfo::GetAccessorIndexByKey(JSTaggedValue key, std::vector<int> &vec)
59 {
60 [[maybe_unused]] EcmaString *str = EcmaString::Cast(key.GetTaggedObject());
61 ASSERT_PRINT(EcmaStringAccessor(str).IsInternString(), "TS class field key is not an intern string");
62
63 uint32_t length = GetNumOfProperties();
64 for (uint32_t i = 0; i < length; ++i) {
65 JSTaggedValue keyVal = GetKey(i);
66 ASSERT_PRINT(keyVal.IsString(), "TS class field key is not a string");
67 if (keyVal == key) {
68 vec.emplace_back(i);
69 }
70 }
71 }
72
TryGetTypeByIndexSign(const uint32_t keyType)73 JSTaggedValue TSObjLayoutInfo::TryGetTypeByIndexSign(const uint32_t keyType)
74 {
75 uint32_t length = GetNumOfProperties();
76 for (uint32_t i = 0; i < length; i++) {
77 if (static_cast<uint32_t>(GetKey(i).GetInt()) == keyType) {
78 JSTaggedValue value = GetTypeId(i);
79 ASSERT(value.IsInt());
80 return value;
81 }
82 }
83 return JSTaggedValue::Undefined();
84 }
85
PushBack(const JSThread * thread,const JSHandle<TSObjLayoutInfo> & oldLayout,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)86 JSHandle<TSObjLayoutInfo> TSObjLayoutInfo::PushBack(const JSThread *thread,
87 const JSHandle<TSObjLayoutInfo> &oldLayout,
88 const JSHandle<JSTaggedValue> &key,
89 const JSHandle<JSTaggedValue> &value)
90 {
91 if (oldLayout->GetNumOfProperties() < oldLayout->GetPropertiesCapacity()) {
92 oldLayout->AddProperty(thread, key.GetTaggedValue(), value.GetTaggedValue());
93 return oldLayout;
94 }
95
96 JSHandle<TSObjLayoutInfo> newLayout = ExtendTSObjLayoutInfo(thread, oldLayout);
97 newLayout->AddProperty(thread, key.GetTaggedValue(), value.GetTaggedValue());
98 return newLayout;
99 }
100
ExtendTSObjLayoutInfo(const JSThread * thread,const JSHandle<TSObjLayoutInfo> & oldLayout,JSTaggedValue initVal)101 JSHandle<TSObjLayoutInfo> TSObjLayoutInfo::ExtendTSObjLayoutInfo(const JSThread *thread,
102 const JSHandle<TSObjLayoutInfo> &oldLayout,
103 JSTaggedValue initVal)
104 {
105 uint32_t oldLength = oldLayout->GetPropertiesCapacity();
106 ASSERT(oldLength == oldLayout->GetNumOfProperties());
107 uint32_t arrayLength = TSObjLayoutInfo::ComputeArrayLength(TSObjLayoutInfo::ComputeGrowCapacity(oldLength));
108 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
109 JSHandle<TSObjLayoutInfo> newLayout(factory->ExtendArray(JSHandle<TaggedArray>(oldLayout), arrayLength, initVal));
110 newLayout->SetNumOfProperties(thread, oldLength);
111 return newLayout;
112 }
113 } // namespace panda::ecmascript
114