1 /*
2 * Copyright (c) 2023 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
16 #ifndef ECMASCRIPT_SUBTYPING_OPERATOR_INL_H
17 #define ECMASCRIPT_SUBTYPING_OPERATOR_INL_H
18
19 #include "ecmascript/subtyping_operator.h"
20
21 namespace panda::ecmascript {
22 template<class T>
GetElementByKey(const JSThread * thread,T * obj,JSTaggedValue key)23 int SubtypingOperator::GetElementByKey(const JSThread *thread, T *obj, JSTaggedValue key)
24 {
25 if constexpr(std::is_same_v<T, TSObjLayoutInfo>) {
26 return obj->GetElementIndexByKey(key);
27 } else if constexpr(std::is_same_v<T, VTable>) {
28 return obj->GetTupleIndexByName(key);
29 } else if constexpr(std::is_same_v<T, JSHClass>) {
30 LayoutInfo *objLayout = LayoutInfo::Cast(obj->GetLayout().GetTaggedObject());
31 return objLayout->FindElementWithCache(thread, obj, key, obj->NumberOfProps());
32 }
33 return -1;
34 }
35
36 template<class T>
IsLegalElement(const JSThread * thread,T * obj,JSTaggedValue key,JSTaggedValue expectedType)37 bool SubtypingOperator::IsLegalElement(const JSThread *thread, T *obj, JSTaggedValue key, JSTaggedValue expectedType)
38 {
39 // when the property of the subtyping does not exist on the parent class,
40 // or exists and has the same type, the property is considered legal
41 if constexpr(std::is_same_v<T, TSObjLayoutInfo>) {
42 int index = obj->GetElementIndexByKey(key);
43 if (index != -1 && obj->GetTypeId(index) != expectedType) {
44 return false;
45 }
46 } else if constexpr(std::is_same_v<T, VTable>) {
47 int index = obj->GetTupleIndexByName(key);
48 if (index != -1) {
49 // type: ture --> isAccessor, false --> normal function
50 ASSERT(expectedType.IsBoolean());
51 bool isAccessor = expectedType.IsTrue();
52 if (obj->IsAccessor(index) != isAccessor) {
53 return false;
54 }
55 }
56 } else if constexpr(std::is_same_v<T, JSHClass>) {
57 LayoutInfo *objLayout = LayoutInfo::Cast(obj->GetLayout().GetTaggedObject());
58 int index = objLayout->FindElementWithCache(thread, obj, key, obj->NumberOfProps());
59 if (index != -1) {
60 // type: ture --> isAccessor, false --> normal function
61 ASSERT(expectedType.IsBoolean());
62 PropertyAttributes attr(objLayout->GetAttr(index));
63 bool isAccessor = expectedType.IsTrue();
64 if (attr.IsAccessor() != isAccessor) {
65 return false;
66 }
67 }
68 }
69 return true;
70 }
71
72 template<class T>
GetLength(T * obj)73 uint32_t SubtypingOperator::GetLength(T *obj)
74 {
75 if constexpr(std::is_same_v<T, TSObjLayoutInfo>) {
76 return obj->GetNumOfProperties();
77 } else {
78 return 0;
79 }
80 }
81
82 template<class T>
GetKey(T * obj,uint32_t index)83 JSTaggedValue SubtypingOperator::GetKey(T *obj, uint32_t index)
84 {
85 if constexpr(std::is_same_v<T, TSObjLayoutInfo>) {
86 return obj->GetKey(index);
87 } else {
88 return JSTaggedValue::Undefined();
89 }
90 }
91
92 template<class T>
GetType(const JSThread * thread,T * obj,uint32_t index,ConditionType ConditionType)93 JSTaggedValue SubtypingOperator::GetType(const JSThread *thread, T *obj, uint32_t index, ConditionType ConditionType)
94 {
95 if constexpr(std::is_same_v<T, TSObjLayoutInfo>) {
96 if (ConditionType == ConditionType::SUB_LOCAL_CONTAIN_SUP_LOCAL) {
97 return obj->GetTypeId(index);
98 } else if (ConditionType == ConditionType::SUB_VTABLE_CONTAIN_SUP_VTABLE) {
99 TSManager *tsManager = const_cast<JSThread*>(thread)->GetCurrentEcmaContext()->GetTSManager();
100 GlobalTSTypeRef typeGT = GlobalTSTypeRef(obj->GetTypeId(index).GetInt());
101 return JSTaggedValue(tsManager->IsGetterSetterFunc(typeGT));
102 }
103 }
104 return JSTaggedValue::Undefined();
105 }
106
107 template<class Suber, class Super>
SubtypingCondition(const JSThread * thread,Suber * suber,Super * super,ConditionType ConditionType)108 bool SubtypingOperator::SubtypingCondition(const JSThread *thread, Suber *suber, Super *super,
109 ConditionType ConditionType)
110 {
111 uint32_t len = GetLength(suber);
112 for (uint32_t index = 0; index < len; ++index) {
113 JSTaggedValue key = GetKey(suber, index);
114 ASSERT(!key.IsUndefined());
115 if (ConditionType == ConditionType::SUB_LOCAL_CONTAIN_SUP_LOCAL ||
116 ConditionType == ConditionType::SUB_VTABLE_CONTAIN_SUP_VTABLE) {
117 JSTaggedValue type = GetType(thread, suber, index, ConditionType);
118 ASSERT(!type.IsUndefined());
119 if (!IsLegalElement(thread, super, key, type)) {
120 return false;
121 }
122 } else {
123 if (GetElementByKey(thread, super, key) != -1) {
124 return false;
125 }
126 }
127 }
128 return true;
129 }
130 } // namespace panda::ecmascript
131 #endif // ECMASCRIPT_SUBTYPING_OPERATOR_INL_H