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_H 17 #define ECMASCRIPT_SUBTYPING_OPERATOR_H 18 19 #include "ecmascript/ts_types/ts_type.h" 20 21 namespace panda::ecmascript { 22 class SubtypingOperator { 23 public: 24 static constexpr uint32_t DEFAULT_SUPERS_CAPACITY = 4; 25 26 static bool CheckBaseClass(const JSThread *thread, const JSHandle<TSClassType> &classType); 27 28 static bool CheckSubtyping(const JSThread *thread, const JSHandle<TSClassType> &classType); 29 30 static void MergeClassField(const JSThread *thread, const JSHandle<TSClassType> &classType); 31 32 static void FillTSInheritInfo(JSThread *thread, const JSHandle<TSClassType> &classType, 33 const JSHandle<JSHClass> &ihcHandle); 34 35 static void GenVTable(const JSThread *thread, const JSHandle<JSHClass> &ihcHandle, 36 const JSHandle<JSHClass> &phcHandle, const JSHandle<JSHClass> &eIhcHandle); 37 38 static void TryMaintainTSSubtyping(const JSThread *thread, const JSHandle<JSHClass> &oldHClass, 39 JSHandle<JSHClass> &newHClass, const JSHandle<JSTaggedValue> &key); 40 41 static bool TryMaintainTSSubtypingOnPrototype(const JSThread *thread, const JSHandle<JSHClass> &hclass, 42 const JSHandle<JSTaggedValue> &key); 43 44 private: 45 static constexpr uint8_t MAX_LEVEL = 1 << JSHClass::LEVEL_BTTFIELD_NUM; 46 47 /* rule check operations will check whether the properties in the Local and Vtable 48 * of TSHClass meet the requirements for establishing a TSHClass chain.The local 49 * properties refer to the properties that exists on the object instance, and the 50 * vtable properties refers to the properties of its ancestor prototype objects 51 * (such as accessor, method, and ordinary property in special cases) 52 * 53 * if B extends A 54 * specific requirements are as follows (N: name, T: type): 55 * 1. local_N(B) ∩ vtable_N(B) = ∅ 56 * 2. local(A) ⊆ local(B) 57 * 3. vtable_NT(A) ⊆ vtable_NT(B) 58 * 4. local_N(A) ∩ vtable_N(B) = ∅ 59 * 5. vtable_N(A) ∩ local_N(B) = ∅ 60 */ 61 enum class ConditionType : uint8_t { 62 NO_OVERLAP_SUB_LOCAL_SUB_VTABLE = 0, 63 SUB_LOCAL_CONTAIN_SUP_LOCAL, 64 SUB_VTABLE_CONTAIN_SUP_VTABLE, 65 NO_OVERLAP_SUP_LOCAL_SUB_VTABLE, 66 NO_OVERLAP_SUP_VTABLE_SUB_LOCAL, 67 }; 68 69 template<class T> 70 static int GetElementByKey(const JSThread *thread, T *obj, JSTaggedValue key); 71 72 template<class T> 73 static bool IsLegalElement(const JSThread *thread, T *obj, JSTaggedValue key, JSTaggedValue expectedType); 74 75 template<class T> 76 static uint32_t GetLength(T *obj); 77 78 template<class T> 79 static JSTaggedValue GetKey(T *obj, uint32_t index); 80 81 template<class T> 82 static JSTaggedValue GetType(const JSThread *thread, T *obj, uint32_t index, ConditionType ConditionType); 83 84 template<class Suber, class Super> 85 static bool SubtypingCondition(const JSThread *thread, Suber *suber, Super *super, ConditionType ConditionType); 86 87 static void AddSuper(const JSThread *thread, const JSHandle<JSHClass> &iHClass, 88 const JSHandle<JSHClass> &extendHClass); 89 }; 90 } // namespace panda::ecmascript 91 92 #endif // ECMASCRIPT_SUBTYPING_OPERATOR_H 93