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_VTABLE_H 17 #define ECMASCRIPT_VTABLE_H 18 19 #include "ecmascript/weak_vector.h" 20 21 namespace panda::ecmascript { 22 class VTable : public TaggedArray { 23 public: 24 enum TupleItem { 25 NAME = 0, 26 TYPE, 27 OWNER, 28 OFFSET, 29 30 ITEM_NUM, 31 ITEM_FIRST = NAME, 32 ITEM_LAST = OFFSET, 33 }; 34 35 class Tuple { 36 public: GetItem(TupleItem item)37 JSHandle<JSTaggedValue> GetItem(TupleItem item) const 38 { 39 return items_[item]; 40 } 41 private: 42 // only allow VTable to create a instance of Tuple Tuple(const CVector<JSHandle<JSTaggedValue>> & vec)43 explicit Tuple(const CVector<JSHandle<JSTaggedValue>> &vec) : items_(std::move(vec)) {} 44 CVector<JSHandle<JSTaggedValue>> items_; 45 46 friend class VTable; 47 }; 48 49 enum TypeKind { 50 FUNCTION = 0, 51 ACCESSOR, 52 NORMAL 53 }; 54 55 static constexpr uint32_t TUPLE_SIZE = 4; 56 57 CAST_CHECK(VTable, IsTaggedArray); 58 59 static Tuple CreateTuple(const JSThread *thread, JSTaggedValue phc, 60 const JSHandle<JSTaggedValue> &owner, uint32_t propIndex); 61 62 static JSHandle<VTable> Copy(const JSThread *thread, const JSHandle<VTable> &vtable); 63 GetNumberOfTuples()64 uint32_t GetNumberOfTuples() const 65 { 66 return GetLength() / TUPLE_SIZE; 67 } 68 GetTupleItem(uint32_t tupleIdx,TupleItem kind)69 JSTaggedValue GetTupleItem(uint32_t tupleIdx, TupleItem kind) const 70 { 71 return Get(tupleIdx * TUPLE_SIZE + kind); 72 } 73 IsAccessor(uint32_t tupleIdx)74 bool IsAccessor(uint32_t tupleIdx) const 75 { 76 TypeKind type = static_cast<TypeKind>(GetTupleItem(tupleIdx, TupleItem::TYPE).GetInt()); 77 return type == TypeKind::ACCESSOR; 78 } 79 80 Tuple GetTuple(const JSThread *thread, uint32_t tupleIdx) const; 81 82 void SetByIndex(const JSThread *thread, uint32_t idx, const VTable::Tuple &tuple); 83 84 void Trim(const JSThread *thread, uint32_t newLength); 85 86 int GetTupleIndexByName(JSTaggedValue val) const; 87 88 bool Find(JSTaggedValue val) const; 89 90 DECL_DUMP() 91 }; 92 } // namespace panda::ecmascript 93 94 #endif // ECMASCRIPT_VTABLE_H