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
TryGetTypeByIndexSign(const uint32_t keyType)58 JSTaggedValue TSObjLayoutInfo::TryGetTypeByIndexSign(const uint32_t keyType)
59 {
60 uint32_t length = GetNumOfProperties();
61 for (uint32_t i = 0; i < length; i++) {
62 if (static_cast<uint32_t>(GetKey(i).GetInt()) == keyType) {
63 JSTaggedValue value = GetTypeId(i);
64 ASSERT(value.IsInt());
65 return value;
66 }
67 }
68 return JSTaggedValue::Undefined();
69 }
70
PushBack(const JSThread * thread,const JSHandle<TSObjLayoutInfo> & oldLayout,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)71 JSHandle<TSObjLayoutInfo> TSObjLayoutInfo::PushBack(const JSThread *thread,
72 const JSHandle<TSObjLayoutInfo> &oldLayout,
73 const JSHandle<JSTaggedValue> &key,
74 const JSHandle<JSTaggedValue> &value)
75 {
76 if (oldLayout->GetNumOfProperties() < oldLayout->GetPropertiesCapacity()) {
77 oldLayout->AddProperty(thread, key.GetTaggedValue(), value.GetTaggedValue());
78 return oldLayout;
79 }
80
81 JSHandle<TSObjLayoutInfo> newLayout = ExtendTSObjLayoutInfo(thread, oldLayout);
82 newLayout->AddProperty(thread, key.GetTaggedValue(), value.GetTaggedValue());
83 return newLayout;
84 }
85
ExtendTSObjLayoutInfo(const JSThread * thread,const JSHandle<TSObjLayoutInfo> & oldLayout,JSTaggedValue initVal)86 JSHandle<TSObjLayoutInfo> TSObjLayoutInfo::ExtendTSObjLayoutInfo(const JSThread *thread,
87 const JSHandle<TSObjLayoutInfo> &oldLayout,
88 JSTaggedValue initVal)
89 {
90 uint32_t oldLength = oldLayout->GetPropertiesCapacity();
91 ASSERT(oldLength == oldLayout->GetNumOfProperties());
92 uint32_t arrayLength = TSObjLayoutInfo::ComputeArrayLength(TSObjLayoutInfo::ComputeGrowCapacity(oldLength));
93 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
94 JSHandle<TSObjLayoutInfo> newLayout(factory->ExtendArray(JSHandle<TaggedArray>(oldLayout), arrayLength, initVal));
95 newLayout->SetNumOfProperties(thread, oldLength);
96 return newLayout;
97 }
98 } // namespace panda::ecmascript
99